PDA

View Full Version : How to resize a window after having added widgets at runtime



dictoon
1st September 2010, 13:32
Hello,

I have a window (it's a QWidget, not a main window) that contains a QScrollArea, that itself contains a QFormLayout with widgets in it.

The following scenario works: I populate the QFormLayout with widgets during the construction of the window, then I resize the window using resize(sizeHint()), and finally I present the window using showNormal() followed by activateWindow().

Now, I'm trying to add more widgets to the QFormLayout at runtime, after the window was presented. The widgets appear correctly, but the resize(sizeHint()) trick doesn't enlarge the window; instead, the QScrollArea now shows a vertical scrollbar.

What it the correct way to basically tell the QScrollArea to *not* show a scrollbar, but to instead resize the window so that all the widgets become visible?

Cheerz,
Franz

wysota
1st September 2010, 14:18
If you don't want QScrollArea to show a scroll bar then why are you using QScrollArea in the first place? The whole point of this widget is to show scrollbars when content doesn't fit onto the display.

dictoon
1st September 2010, 14:27
Sorry, I wasn't clear on this point. I'm using a QScrollArea because (1) I want the user to be able to resize the window freely, possibly to a size that cannot accomodate all the widgets, and (2) there might be way too many widgets to display, so at some point I might have to limit the height of the window anyway, and let the user scroll through the widgets using the scrollbars.

To illustrate a bit my problem:


The window in its initial state, with the initial set of widgets: http://appleseedhq.net/stuff/entityeditorwindow_initial.jpg
The window after new widgets were added at runtime to the QFormLayout inside the QScrollArea (a vertical scrollbar is now visible, the objective is to get rid of it by enlarging the window): http://appleseedhq.net/stuff/entityeditorwindow_addedwidgets.jpg
The window as it was resized by the user (and that can now accomodate more widgets without having to be enlarged): http://appleseedhq.net/stuff/entityeditorwindow_resized.jpg


Cheerz,
Franz

wysota
1st September 2010, 15:04
Sorry, I wasn't clear on this point. I'm using a QScrollArea because (1) I want the user to be able to resize the window freely, possibly to a size that cannot accomodate all the widgets, and (2) there might be way too many widgets to display, so at some point I might have to limit the height of the window anyway, and let the user scroll through the widgets using the scrollbars.
So you can't possibly expect sizeHint() to do what you want if you want to allow widgets to be scrolled. You can calculate the proper height yourself but don't expect Qt to do it for you. And expect users to be mad when they resize the window and after a while you change its size to the default one from within your program's code.

To illustrate a bit my problem:



The window in its initial state, with the initial set of widgets: http://appleseedhq.net/stuff/entityeditorwindow_initial.jpg
The window after new widgets were added at runtime to the QFormLayout inside the QScrollArea (a vertical scrollbar is now visible, the objective is to get rid of it by enlarging the window): http://appleseedhq.net/stuff/entityeditorwindow_addedwidgets.jpg
The window as it was resized by the user (and that can now accomodate more widgets without having to be enlarged): http://appleseedhq.net/stuff/entityeditorwindow_resized.jpg

So get rid of the scroll area until you need it and everything will work out of the box.

dictoon
1st September 2010, 15:42
So you can't possibly expect sizeHint() to do what you want if you want to allow widgets to be scrolled. You can calculate the proper height yourself but don't expect Qt to do it for you. And expect users to be mad when they resize the window and after a while you change its size to the default one from within your program's code.

Users won't get "mad" because I only plan to increase the height of the window if the current height is too small to accomodate the new widgets (and I'm never changing the width). What would be a better way to present a variable number of widgets in a window?


So get rid of the scroll area until you need it and everything will work out of the box.

I thought of that but, unless I'm mistaking, I still need the scroll area in case the user resizes the window to a smaller size.

Look, I'm not expecting Qt to do the magic for me. I'm just a bit confused about what is the proper way to adjust the height of my window after having added widgets at runtime. I basically want to simulate what Qt does when I present the window with its initial set of widgets: in this case, Qt has no problem to resize the window appropriately (because sizeHint() returns the right value).

Thanks for your help.

Cheers,
Franz

wysota
1st September 2010, 16:06
Users won't get "mad" because I only plan to increase the height of the window if the current height is too small to accomodate the new widgets (and I'm never changing the width).
And if I (as a user) want to reduce the height of the window on purpose and you constantly keep increasing it back? I really hate when programmers are trying to be smarter than users of their software...


What would be a better way to present a variable number of widgets in a window?
If you have a scroll are then rely on it and the sanity of the user.



I'm just a bit confused about what is the proper way to adjust the height of my window after having added widgets at runtime.
The proper way is to use layouts that will do it for you.

because sizeHint() returns the right value
The thing is sizeHint() returns the right value all the time. If you add a widget to the scroll area inside the controlled widget, it doesn't cause a necessity to increase the window's size, it's just you as the programmer who sees this necessity. There is nothing stopping you from reimplementing sizeHint() for your widget and defining it differently. Actually you should provide your own layout class here but I really don't see how would you detect that you shouldn't be increasing the window size anymore and should allow the scroll area to do its job...

dictoon
1st September 2010, 16:11
And if I (as a user) want to reduce the height of the window on purpose and you constantly keep increasing it back? I really hate when programmers are trying to be smarter than users of their software...

If you have a scroll are then rely on it and the sanity of the user.

Fair enough. Maybe I'm just too focused on this issue because the initial size of the window is too small, and I should rather make the window larger initially and leave the user in full control of the window's dimensions after that...


The proper way is to use layouts that will do it for you.

The thing is sizeHint() returns the right value all the time. If you add a widget to the scroll area inside the controlled widget, it doesn't cause a necessity to increase the window's size, it's just you as the programmer who sees this necessity. There is nothing stopping you from reimplementing sizeHint() for your widget and defining it differently. Actually you should provide your own layout class here but I really don't see how would you detect that you shouldn't be increasing the window size anymore and should allow the scroll area to do its job...

I see. Thanks for the heads-up.

Cheerz,
Franz