为什么 adjustSize 不调整 Qt 中 MainWindow 的大小?
Why adjustSize doesn't resize MainWindow in Qt?
我有一个简单的应用程序。在 MainWindow 的构造函数中,我有:
_someWidget = new someWidgetClass(this);
_someWidget ->setFixedSize(700,700);
_someWidget ->move(50,50);
wid = new QWidget(this);
wid->move(800,800);
wid->setFixedSize(100,100);
centralWidget()->adjustSize();
adjustSize();
我想像那样调整MainWindow的大小,他的右下角应该是wid
的右下角,所以我想将MainWindow的大小调整到他的内容。但是 adjustSize
不起作用。
我尝试在 someWidgetClass
和 return 他的尺码中添加 sizeHint()
方法,但这也无济于事。
你应该为 centralWidget
设置一种布局,例如,我用 QGridLayout
测试它。然后在该布局中添加您的小部件:
auto _someWidget = new QWidget(this);
_someWidget->move(50, 50);
_someWidget->setFixedSize(700, 700);
centralWidget()->layout()->addWidget(_someWidget);
auto wid = new QWidget(this);
wid->move(800, 800);
wid->setFixedSize(100, 100);
centralWidget()->layout()->addWidget(wid);
centralWidget()->adjustSize();
adjustSize();
关于 adjustSize 函数:
Adjusts the size of the widget to fit its contents. This function uses
sizeHint() if it is valid, i.e., the size hint's width and height are
>= 0
. Otherwise, it sets the size to the children rectangle that covers all child widgets (the union of all child widget rectangles).
For windows, the screen size is also taken into account. If the
sizeHint() is less than (200, 100) and the size policy is expanding,
the window will be at least (200, 100). The maximum size of a window
is 2/3 of the screen's width and height.
我有一个简单的应用程序。在 MainWindow 的构造函数中,我有:
_someWidget = new someWidgetClass(this);
_someWidget ->setFixedSize(700,700);
_someWidget ->move(50,50);
wid = new QWidget(this);
wid->move(800,800);
wid->setFixedSize(100,100);
centralWidget()->adjustSize();
adjustSize();
我想像那样调整MainWindow的大小,他的右下角应该是wid
的右下角,所以我想将MainWindow的大小调整到他的内容。但是 adjustSize
不起作用。
我尝试在 someWidgetClass
和 return 他的尺码中添加 sizeHint()
方法,但这也无济于事。
你应该为 centralWidget
设置一种布局,例如,我用 QGridLayout
测试它。然后在该布局中添加您的小部件:
auto _someWidget = new QWidget(this);
_someWidget->move(50, 50);
_someWidget->setFixedSize(700, 700);
centralWidget()->layout()->addWidget(_someWidget);
auto wid = new QWidget(this);
wid->move(800, 800);
wid->setFixedSize(100, 100);
centralWidget()->layout()->addWidget(wid);
centralWidget()->adjustSize();
adjustSize();
关于 adjustSize 函数:
Adjusts the size of the widget to fit its contents. This function uses sizeHint() if it is valid, i.e., the size hint's width and height are
>= 0
. Otherwise, it sets the size to the children rectangle that covers all child widgets (the union of all child widget rectangles). For windows, the screen size is also taken into account. If the sizeHint() is less than (200, 100) and the size policy is expanding, the window will be at least (200, 100). The maximum size of a window is 2/3 of the screen's width and height.