PDA

View Full Version : Automatic Layout Computation for Preferences Dialog



apatwork
30th August 2018, 16:37
Hi there,

I want to implement a preferences dialog window that automagically resizes depending on the contents it shows. I want the dialog to behave the same way as the popup window that shows up when one executes the action

"Tools" -> "Options"

in the qtcreator IDE. My assumption is that this dialog consists mainly of

- a QListView showing a list of option categories on the left and right besides it

- a QStackedWidget which displays the options for the currently selected category in the option categories list.

The preferences for a certain category are provided as QWidgets that are shown in the tabs of a QTabWidget.
I noticed that the qtcreator's options dialog behaves very smart regarding its size: Initially the dialog window's size is as big that any of the available categories' tab pages can be shown inside the dialog without the need for a scroll bar (shown vertically or horizontally).
If the dialog's size is reduced by the user then there is a minimum size that cannot be resized below.
When afterwards the category is changed the dialog window automatically enlarges to provide enough space for display of the other category's options. Upon further category changes the size of the dialog is enlarged if necessary but never scaled down in case a preference tab needs less space than the dialog window currently occupies.
I'd like to implement the same functionality for my preferences dialog but could not yet figure out how to accomplish that behavior.
For the initial display of the dialog it must be somehow possible to iterate over all the categories and their associated preferences and calculate the required size for the display of each of them and finally set the dialog's size to the biggest required size encountered. Think, this initial calculation must take place in QDialog's overloaded slot method open().
And for the calculation of the optimal size for display of a certain category's preference pages I have to compute that size and compare it with the current size of the dialog window and enlarge it if necessary.
This should happen in a slot method that is connected with the currentChanged signal of the QStackedWidget instance.

But how are all these computations to be implemented? I'm a bit confused of the many geometry-related properties in the QWidget class (which seems to be responsible for layout calculations).

Could anyone enlighten me?

d_stranz
30th August 2018, 18:17
Think, this initial calculation must take place in QDialog's overloaded slot method open().

Better to put it in the QDialog::showEvent(). At this point, Qt has completely laid out the dialog and it is ready to be shown on screen. Prior to that point, layout is still occurring and you cannot be guaranteed that widgets contained within it have been properly sized and laid out.

In the same way, I don't think you can correctly determine the sizes of pages in the stack widget that have not yet been shown until they actually execute their own showEvent(). Once this happens, you may be able to use the page's sizeHint() to determine the preferred size, and then resize your dialog accordingly.

In most cases, if you use layouts properly in a QDialog and have not set a maximum size constraint, the dialog will expand itself automatically to hold its content. It usually will not shrink if it contains stacked pages that are smaller than the largest one shown so far.

apatwork
31st August 2018, 15:23
Thanks a lot! Using the showEvent() method for placing the code for the size calculation did the trick:

setMinimumSize(sizeHint());

made the dialog big enough that all the stacked widgets could be shown with proper size. Need to add that I created the different stacked widgets in qtdesigner and added them in qtdesigner to the QStackedWidget:

12959