Search code examples
c++qtresizeqdialogqtwidgets

Enable resizing on QWidget


I want a resize feature in a QWidget using Qt, like the one shown in the image below.

enter image description here

I have used following tried following ways:

using QSizeGrip, setSizeGripEnabled


Solution

  • For completeness I'm showing two examples: with and without the Qt Designer.


    Example using Qt Designer

    designer

    Check the sizeGripEnabled property:

    properties

    Preview from within the Qt Designer (Form > Preview...):

    preview

    Minimal application to show the dialog:

    #include <QtWidgets/QApplication>
    #include <QDialog>
    #include "ui_DialogButtonBottom.h"
    
    class Dialog : public QDialog {
    public:
      Dialog(QWidget* parent = nullptr) :
        QDialog(parent) {
        ui.setupUi(this);
      }
    
    private:
      Ui::Dialog ui;
    };
    
    int main(int argc, char* argv[])
    {
      QApplication a(argc, argv);
    
      Dialog dlg;
      return dlg.exec();
    }
    

    Resut

    result


    Without Qt Designer

    #include <QtWidgets/QApplication>
    #include <QDialog>
    
    class Dialog : public QDialog {
    public:
      Dialog(QWidget* parent = nullptr) :
        QDialog(parent) {
        setWindowTitle("Example");
        setSizeGripEnabled(true);
      }
    };
    
    int main(int argc, char* argv[])
    {
      QApplication a(argc, argv);
    
      Dialog dlg;
      return dlg.exec();
    }
    

    Result

    result


    Update to include Frameless mode

    Adding the Frameless windows hint doesn't change anything: it works correctly. Obviously, there is no frame so resize/move methods provided by the windows manager are not available.

    #include <QtWidgets/QApplication>
    #include <QDialog>
    
    class Dialog : public QDialog {
    public:
      Dialog(QWidget* parent = nullptr, Qt::WindowFlags flags = 0) :
        QDialog(parent, flags) {
        setWindowTitle("Example");
        setSizeGripEnabled(true);
      }
    };
    
    int main(int argc, char* argv[])
    {
      QApplication a(argc, argv);
    
      Dialog dlg(nullptr, Qt::FramelessWindowHint); // frameless
      return dlg.exec();
    }
    

    Result

    frameless


    As all the options are working straightforwardly, I'd suggest you to carefully review your code/UI design for things like setting a maximum/minimum size (if both are the same, the grip will still be available but won't change the size at all).