Qt Layout, resize to minimum after widget size cha

2020-02-17 09:39发布

Basically I've got a QGridLayout with a few widgets in it. The important ones are 2 labels, which I use for drawing images to the screen. Well, if the user wants, he can change the resolution of the incoming images, thus, forcing the Labels to resize.

Let's assume the initial size of the label is 320x240. The user changes the VideoMode to 640x480, the label and the entire GUI resizes perfectly. But when the user switches back to 320x240, the label shrinks, but the Layout/Window does NOT.

I've played around with sizePolicies and sizeHints, and resize(0,0), but nothing did the trick. Could somebody help me with this?

Here some screenshots to clarify the problem:

Here, everything is okay still, 640 x 480 resizes perfectly but here, the layout is too big.

4条回答
可以哭但决不认输i
2楼-- · 2020-02-17 09:54

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

查看更多
在下西门庆
3楼-- · 2020-02-17 10:00

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();

 }
}
查看更多
爷、活的狠高调
4楼-- · 2020-02-17 10:02

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);
查看更多
来,给爷笑一个
5楼-- · 2020-02-17 10:07

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);
查看更多
登录 后发表回答