PDA

View Full Version : Toggling the size of a window



Jeffb
6th August 2011, 07:42
Hi Guys

I have a window that contains 2 widgets in a vertical splitter and opens with only the top widget showing with no space below it.
I want to be able to:
1. Click a button and have the bottom widget show which causes the window to increase in height by extending downwards, to accommodate the second widget. (I can do this.)
2. When the button is clicked a second time, I want the second widget to hide again and the window to resize to its original size - no problem getting it to hide, but the window won't resize.

Any help appreciated.

Jeff

Dong Back Kim
6th August 2011, 10:27
Hi Guys

I have a window that contains 2 widgets in a vertical splitter and opens with only the top widget showing with no space below it.
I want to be able to:
1. Click a button and have the bottom widget show which causes the window to increase in height by extending downwards, to accommodate the second widget. (I can do this.)
2. When the button is clicked a second time, I want the second widget to hide again and the window to resize to its original size - no problem getting it to hide, but the window won't resize.

Any help appreciated.

Jeff

Hi Jeff,

Since you can show/hide the bottom one by catching the button click event, you must be able to resize the main window as well. It should be very easy and simple. All you need is to have the pointer of the base window and resizing window widget by using resize() method.

Regards,

Jeffb
6th August 2011, 11:41
@Dong
Yep - I've tried that. I got the size of the main window before showing the bottom widget. And then after hiding the bottom widget call resize on the main window. It simply does not resize.
I have no idea why.

Jeff

Dong Back Kim
6th August 2011, 14:37
@Dong
Yep - I've tried that. I got the size of the main window before showing the bottom widget. And then after hiding the bottom widget call resize on the main window. It simply does not resize.
I have no idea why.

Jeff

Hi Jeff,

hmmm that's bit weird because I just tried it out myself and it works perfectly.



void MainWindow::on_pushButtonTest_clicked()
{
this->resize(this->width() + 10, this->height() + 10);
}


I could see the window size is increasing every single time I click the button.

Regards,

Jeffb
8th August 2011, 09:44
@Dong

Finally got it solved. Your last reply got me to set up a simple MainWindow to experiment.

Here's the code for anyone who's interested.
Any improvements are welcome.
Remember that this shows and hides a bottom widget where the top and bottom widgets are inside a splitter.
Note: splitterSizes is a QList<int>

In constructor:

// hide the bottom widget
int topWidgetHeight = ui->topWidget->sizeHint().height(); // top widget height
bottomWidgetHeight = 100 + ui->splitter->handleWidth(); // I set the bottomWidgetHeight
splitterSizes << topWidgetHeight << 0;
ui->splitter->setSizes(splitterSizes);
ui->splitter->handle(1)->setUpdatesEnabled(false); // hides the handle - it can't repaint - setHidden(true) doesn't work
bottomWidgetHidden = true;

on_button_Click() method:

void MainWindow::on_ButtonClicked()
{
if (bottomWidgetHidden)
{
// show bottom widget
int topWidgetHeight = ui->topWidget->height();
mainWindowSize = this->size();

splitterSizes.clear();
splitterSizes << topWidgetHeight << bottomWidgetHeight;
ui->splitter->setSizes(splitterSizes);

ui->splitter->handle(1)->setUpdatesEnabled(true); // shows the handle
this->resize(mainWindowSize.width(), mainWindowSize.height() + bottomWidgetHeight);
}
else
{
// hide bottom widget
// get sizes/heights
mainWindowSize = this->size();
int topWidgetHeight = ui->topWidget->height();
bottomWidgetHeight = ui->bottomWidget->height();

// set the widget heights - this maintains the size of the top widget
splitterSizes.clear();
splitterSizes << topWidgetHeight << 0;
ui->splitter->setSizes(splitterSizes);

ui->splitter->handle(1)->setUpdatesEnabled(false); // hides the handle - it can't repaint - setHidden(true) doesn't work
this->resize(mainWindowSize.width(), mainWindowSize.height() - bottomWidgetHeight);
}
bottomWidgetHidden = !bottomWidgetHidden;
}

Cheers
Jeff

Dong Back Kim
10th August 2011, 02:50
Good to hear it works :)