假设有一个QWidget
布局是垂直的。此布局有一个按钮 (bt1) 和另一个布局 ( hLayout
)。
该应用程序可以运行,以便单击 bt1 添加/删除hLayout
.
第一次添加按钮时,窗口变大(宽度增加)以保持新按钮。下一次单击隐藏按钮。但是隐藏按钮不会使窗口缩小。我希望与添加按钮之前的大小相同。
删除按钮时正确缩小窗口的方法是什么?或者更一般地说,使窗口获得足够大小的方法是什么?
As long as doing explicit size adjustments with resize implies to cover all possible events there is lighter and smarter approach to make the widget resize properly automatically. Have a look at QWidget::setSizePolicy and QSizePolicy.
In situation like yours I often use:
// your specific code and size policy *may* differ
widget->setMinimumSize(width, height);
widget->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
and don't interfere with event handlers to resize to specific size in such or many cases.
widget->resize(widget->minimumSize())
从 中删除按钮后只需调用hLayout
。