Hide/Show Qt Widgets depending on radio buttons: The original window size is not retained after reselection.
-
Hi,
I am building a GUI Application for desktop. In my Vierw I have two groupboxes and a radio box with the options "show 1" and "show 2". When I start the application he default value is "show 1" and only 1 groupbox is shown (the other is set to visible) like this:
When I chosse "show 2" the second groupbox appears and the window is resized like this:
If I select "Show 1" again, the second GroupBox disappears and the window is reduced again, but the window is still larger than when the program was started like this. What could be the problem?
Here is my Code:
//mainwindow.cpp constructor: ui->setupUi(this); hideSecondBox(true);
// function for hiding/showing the second groupbox in mainwindow.cpp: void MainWindow::hideSecondBox(bool hide) { if(hide){ window()->resize(530,700); ui->groupBox_second->setVisible(false); } else{ window()->resize(960,700); ui->groupBox_second->setVisible(true); } } //If "show 2" radio button is clicked void MainWindow::on_radioButton_second_clicked() { hideSecond(false); } //If "show 1" radio button is clicked void BluetoothController::on_radioButton_first_clicked() { hideSecond(true); }
Another thing is that the implementation seems somehow unnecessarily long with so many functions. Is there an easier way?
-
Hi
It seems it comes from the fact that there is both a hide/show and
a resize. both triggering layout recalculations.if i do like this, it works.
void MainWindow::hideSecondBox(bool hide) { if (hide) { ui->groupBox_second->setVisible(false); QTimer::singleShot(100, [this] { resize(530, 700); }); } else { ui->groupBox_second->setVisible(true); QTimer::singleShot(100, [this] { resize(960, 700); }); } }
-
@TUStudi
I don't suppose it will alter, but does it help if in yourhide
case you do theresize()
after setting invisible?If you leave
groupBox_second
permanently invisible and just do theresize()
s does it then work correctly?Otherwise, are your boxes on a layout?
-
Hi
Since you are in MainWindow already, using
window()->resize(530,700); seems a bit odd
using
resize(530,700);
will be fine. ( unless you plan to embed the Mainwindow into some other window)About the size
Did you set its start size in the Designer ?
I would try// mainwindow.cpp constructor:
ui->setupUi(this);
resize(530,700); // make sure we have the compressed size
hideSecondBox(true); -
@JonB said in Hide/Show Qt Widgets depending on radio buttons: The original window size is not retained after reselection.:
@TUStudi
I don't suppose it will alter, but does it help if in yourhide
case you do theresize()
after setting invisible?Hey, good approach, but it does not help.
If you leave
groupBox_second
permanently invisible and just do theresize()
s does it then work correctly?Yes, when I set it to invisible permanently and just resize the window, then it works.
Otherwise, are your boxes on a layout?
Yes (BluetoothController is My MainWindow):
So the radio buttons are over my first groupbox and my groupboxes are in parallel like shown above.
@mrjj :
using
resize(530,700);
will be fine.
Thank you for this advice, I did it now like this.
// mainwindow.cpp constructor:
ui->setupUi(this);
resize(530,700); // make sure we have the compressed size
hideSecondBox(true);Unfortunately that does not help.
-
Hi
Ok that's a bit odd since it seems all are in layouts
so not sure what is going on.
So its a stacked + box layout and then 3 groupboxes and and all you do is
hide and re-show groupbox 2 and resize window ? -
@TUStudi
@mrjj will doubtless help you better than I. But for a test, are you able to get rid of yourQStackedWidget
from this example to see if that makes a difference? I have found times where things in aQStackedWidget
are not keen to shrink down again to what they used to be after hiding/showing. Just a thought. -
@mrjj will doubtless help you better than I. But for a test, are you able to get rid of your QStackedWidget from this example to >see if that makes a difference? I have found times where things in a QStackedWidget are not keen to shrink down again to what >they used to be after hiding/showing. Just a thought.
Wel, the problem is that all my content is inside the stacked widget so when I delete the Stackedwidget my whole GUI is gone ;D
@mrjj said in Hide/Show Qt Widgets depending on radio buttons: The original window size is not retained after reselection.:
Hi
Ok that's a bit odd since it seems all are in layouts
so not sure what is going on.
So its a stacked + box layout and then 3 groupboxes and and all you do is
hide and re-show groupbox 2 and resize window ?Well, here is my concept (there are a lot of buttons and boxes and so on in it so I just tried to make it a little bit easy an clearly . So this Boxes with borders are groupboxes. So actually I have 2 big independet groupboxes (the second one is the one I hide/show). And both contains 2 anpther group boxes and a button.
-
Hi
I tried to recreate the situation and i cant reproduce it.
it does go back to old size.--
Ok. but you hide the outer group box ?
so it will hide all children so should not matter.Would it be possible to have the ui file ?
At least i can then check if your version does it for me too. -
Hi
It seems it comes from the fact that there is both a hide/show and
a resize. both triggering layout recalculations.if i do like this, it works.
void MainWindow::hideSecondBox(bool hide) { if (hide) { ui->groupBox_second->setVisible(false); QTimer::singleShot(100, [this] { resize(530, 700); }); } else { ui->groupBox_second->setVisible(true); QTimer::singleShot(100, [this] { resize(960, 700); }); } }