PDA

View Full Version : Maximumsize by layout QMainWindow



captiva
20th February 2009, 12:34
I'm trying to set the maximum size of a qmainwindow by means of layouts / policies. For this I tested some things in Qt Designer.

When:
-I create a simple dialog
-put in there a textedit widget with a certain maximum size
-add a layout to the dialog with layoutSizeConstraint: SetMaximumSize

The dialog behaves as expected, I can resize, till I hit the maximum size of the child widget, then I can maximize the window no further.

When I do the same for a QMainWindow it does not work. The centralwidget stops resizing after hitting the maximum size, but the mainwindow simply grows more border space. (This is probably due to the custom layouthandler of the qmainwindow???)

(I know I can set maximumsize on the qmainwindow, but that is not a solution, because it would involve calculating all the extra border requirements for toolbars and the like. The size policies are used specifically to avoid that.)

Any idea on how to tackle this? (A QT designer example or some code would be great!)

The QT designer settings:

http://www.qtcentre.org/forum/picture.php?albumid=5&pictureid=28

http://www.qtcentre.org/forum/picture.php?albumid=5&pictureid=27

wysota
21st February 2009, 14:55
Unfortunately you have to calculate the area used by toolbars and other main window thingies. It shouldn't be that hard. Just measure the size of the whole main window and substract your central widget size. What remains are all the thingies you need to consider and you can do that at any time you want. Then apply the layout constraint.

captiva
22nd February 2009, 12:24
But that is not really the idea of a managed layout?

If I have to set maximumsize manually, I would need to reset the size each time a user decides to place the toolbar (which is free to replace) somewhere else (e.g. vertically instead of horizontally).

The qmainwindow should honor maximumsize constraint of its centralwidget-layout, like normal dialogs do!

wysota
22nd February 2009, 14:29
A top level window honours constraint of its own layout. The ability doesn't propagate upward, it would be very bad if it did. If you set the constraint on the central widget's layout, it will honour its content's size but it won't propagate upwards to the top level window. If you want such a special feature in this particular case unfortunately you'll have to provide it yourself. A usual case is that size of contents of a widget is dependent on the parent widget, not the other way round. If you start moving toolbars or dock widgets, the central widget will adjust to the size of the whole window, regardless of the size constraint.

captiva
23rd February 2009, 12:20
Thank you for your reply. But by top level window, you mean qmainwindow based classes?

I think upward propagation does work in some cases, because dialogs do honor the maximumsize constraint of a layout+widget placed in them. I can see cases where it makes no sense to resize a window larger than the contents allow, and here, upward propagation saves the user from manually having to set maximumsizes on the top-level window. (and reset them each time something changes, essentially writing a kind of layout handler)

It is the custom layout handler of the qmainwindow which does not honor upward constraints? I understand that top level windows have a more complex layout, with movable toolbars and the like, but it would be quite usefull if they let me access some of the layout policies. (like "always shrink to fit - maximumsize"...)

(to avoid confusion:
-downward constraints, e.g. windowsize limits the size of widgets within the window
-upward constraints, widgets have a maximum size, which could limit the maximum size of a window containing that widget (the window does have a layout assigned to it, with setmaximumsize-policy, and in that case it will follow the sizehint of the containing widgets.)

wysota
23rd February 2009, 19:18
Thank you for your reply. But by top level window, you mean qmainwindow based classes?
No, I mean widgets without the parent - they contain a decoration and can be moved around. The base class is irrelevant here (apart from the simple rule of a thumb that QDialog derived classes are top level unless you force them otherwise).


I think upward propagation does work in some cases, because dialogs do honor the maximumsize constraint of a layout+widget placed in them.
Because they are top-level widgets/windows :)


It is the custom layout handler of the qmainwindow which does not honor upward constraints?
No, it's the mechanism that doesn't propagate upwards. You place a constraint on a concrete and only one layout. If you also place a constraint on a layout of its parent widget, it will adjust.

captiva
24th February 2009, 12:48
Thanks. I'm relatively new to GUI programming, so I had some concepts/terminology mixed up. I think I'm beginning to understand :o

But in this case the conclusion of the story is: it is not useful to put a layout & constraint on the centralwidget because the parent/toplevel qmainwindow has its own layout that will adjust anyway.

(I cannot assign layouts directly to the qmainwindow, so that is what I meant with "qmainwindow has a custom layout handler". I thought I could get similar behaviour as a qdialog toplevel window, by assigning a layouthandler to the centralwidget, but that is not the case. The qmainwindow toplevel handler overrules..)

So the only solution to my problem is to call setmaximumsize, and do some kind of event-detection to set new sizes if a user decides to place a toolbar somewere else. (Do you have some example code lying around? :rolleyes:)

wysota
24th February 2009, 19:41
But in this case the conclusion of the story is: it is not useful to put a layout & constraint on the centralwidget because the parent/toplevel qmainwindow has its own layout that will adjust anyway.
Yes, that's true. You can try applying a constraint to both layouts at once though.