PDA

View Full Version : Resizing height of dialog box dynamically



uz_qt
8th November 2013, 11:53
hello,

I have a dialog box with a list 3 QCheckBoxes items inside it. The visibility of last two items is connected to first QCheckbox.
So, when the checkbox is 'on', the two items are visible and 'off' makes them invisible.
By default, they are set invisible. So, when turn first checkbox 'on', the items appear and the height of dialog box is increased. So far fine.

But when I turn it 'off', the items disappear leaving empty space in the layout (since the dialog box is now larger).
How can I make the window height also resize (get shorter) when items are invisible?

For example:
Since I use a QVBoxLayout as the main layout in my dialog box:


QVBoxLayout *MainLayout = new QVBoxLayout;
MainLayout->addItem( new QSpacerItem(1,1,QSizePolicy::Expanding, QSizePolicy::Expanding));

Like the above lines resize the layout, how can I similarly resize the dialog-box height? Please help.

anda_skoa
8th November 2013, 12:31
For example by calling resize(0, 0) on the dialog and let its layout figure out how much it has to increase its size again.

Cheers,
_

uz_qt
8th November 2013, 12:54
@anda: To shrink the layout, I have written lines in the slot function of my first QCheckBox as follows(lines for toggling visibility of items ans other stuff not shown) :

void MyResizeTab()
{
MainLayout->addItem( new QSpacerItem(1,1,QSizePolicy::Expanding, QSizePolicy::Expanding)); //to shrink the layout
resize(0, 0); //to shrink the dialog
}

It still does not shrink. May be because space is available in the lower part of layout. If I remove QSpacerItem, then the layout does not shrink! What is the way out?

anda_skoa
9th November 2013, 13:43
Is MyResizeTab a slot of the dialog class?
Do you have any spacer items in the layout? Is their size hint small enouht? E.g. 0 or 1?

Cheers,
_

uz_qt
9th November 2013, 14:46
ya, the slot function is in the dialog class.

I actually have 2 sub-layouts which added to the MainLayout. When the checkbox items are set visible, then the items are added vertically in the sub-layout, resulting in increase in the height of sub-layouts, mainlayout and hence the dialog box. Now, when the visibility is turned off, I can shrink back the layout to original size using:

MainLayout->addItem( new QSpacerItem(1,1,QSizePolicy::Expanding, QSizePolicy::Expanding)); //to shrink the layout
But the height of the dialog box remains same (larger than original).
What I want to have is when the items are invisible, the whole dialog box height should also reduce to the original height. In other words, currently the height only increases(when visibility 'on') but not decreases back (when visibility 'off').
How can I make it work in both directions?

anda_skoa
9th November 2013, 15:52
Can you explain to me how adding an expanding spacer is going to result in shrinking?

In my understanding of the English language, which is limited due to not being my mother tounge, expanding is kind of the opposite if shrinking. Also adding doesn't sound like something that would result in less space being used.

Cheers,
_

uz_qt
9th November 2013, 19:00
@anda: You totally got me wrong. Let me explain.
The reason I am adding QSpacerItem is to the layout is to shrink the layout when the objects are invisible. (I may be wrong in doing this).
If I do not add the QSpacerItem, then the items above are spread all over the layout to accommodate the void caused by making the items invisible.
I have added a picture (pict.jpg) which demonstrate the current behavior (steps 1-4) and ideal behavior (steps 5-7).