Qt Layout, resize to minimum after widget size changes

╄→尐↘猪︶ㄣ 提交于 2019-11-30 03:05:15

You need to set the size constraint of the layout holding all your widgets to "SetFixedSize". Although the name doesn't sound like it will work, it ensures that your layout will only use the space it needs. You will not have the problem like you do in your second screenshot.

Example:

mainLayout.setSizeConstraint(QLayout::SetFixedSize);

QLayout::setSizeConstraint(QLayout::SetFixedSize) solves this problem well when you prefer keeping your widget's size fixed at all times--that is, if you'd like it to always be fixed to its "packed" size (which may still vary as the child widgets change size). That is what the "fixed" means there: "fixed" to the correct size, even as the latter varies. (In Qt terms, what I'm calling the "packed" size is simply the widget's sizeHint.)

But a constraint may be too strong a solution in some instances. In particular, if you apply it to a top-level window, then the user will not be free to resize the window. If you don't like that, you can instead perform the "set size to sizeHint" operation instantaneously each time it's needed, rather than imposing it as an unrelenting constraint. The way to do that is to call QWidget::adjustSize().

http://doc.qt.io/qt-5/qwidget.html#adjustSize

Note that if the container whose children are changing size is not the top-level window, then adjustSize() may have to be called recursively on the container and its parents. (In my case I had to do that, anyway. I also tried the size-constraint scheme, and found that applying the constraint at only the topmost level was successful in compacting all levels. I haven't enough knowledge of Qt to comment usefully on these observations, so I merely share them.)

You need to store the original size of your widget parent window before applying any changes to the layout and restore it when the user switches back to the original.

Notice that you need to work with the widget parent window size and not the widget parent size.

in your widget before applying the layout changes:

minimumWindowSize = this->window().size();

when you finished reorganizing the widget to the compact size

this->window().resize(minimumWindowSize);

So that is exactly what i'm doing in mu project. Resolution os doesn't matter. I have only to have a widget for rendering video, or image in your case.

void MainWindow::resizeEvent(QResizeEvent* event)
 {
   QMainWindow::resizeEvent(event);
   if ((player != 0) && ((player->isPlaying()) || player->isLoaded() || player>isLoaded())){
     renderer->resize(ui->mainVideoWidget->width(),ui->mainVideoWidget->height());
     resizeFilter();

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