Enable resizing on QWidget

别说谁变了你拦得住时间么 提交于 2020-12-12 03:18:38

问题


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


回答1:


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).



来源:https://stackoverflow.com/questions/44302704/enable-resizing-on-qwidget

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!