Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Runtime layout



  • Hi!

    In my application there is a QFrame, say F, and inside it there are 3 other QFrame objects, say A, B and C. F's layout is vertical, with A at the top, followed by B and C.
    In A there are two QRadionButton objects, and depending on the user choice, B or C should be visible. That does not seem hard to do, but the layout in F is broken when the Window is displayed, and is only fixed when I click one of the QRadionButton in A, as this code is executed:

    {
      if (ui->rdbA->isChecked()) {
        ui->fraB->setVisible(false);
        ui->fraA->setVisible(true);
        ui->fraF->layout()->removeWidget(ui->fraB);
        ui->fraF->layout()->addWidget(ui->fraA);
      } else {
        ui->fraB->setVisible(true);
        ui->fraA->setVisible(false);
        ui->fraF->layout()->addWidget(ui->fraB);
        ui->fraF->layout()->removeWidget(ui->fraA);
      }
      this->setGeometry(parentWidget()->x(),
                        parentWidget()->y(),
                        parentWidget()->width(),
                        parentWidget()->height());
    }
    

    Could anyone, please, give me any idea, or reference, on how to do it?

    Thanks!



  • @canellas
    What do you mean by "but the layout in F is broken when the Window is displayed"?

    It will only correctly show one frame when the code you show gets executed, as that sets up to view one frame, correctly. (Or is your question about a specific bit of the code?) So that's reasonable, no?

    However, before you spend time on this, would using a QStackedWidget (https://doc.qt.io/qt-5/qstackedwidget.html), which is designed for swapping between multiple widgets so only one is shown at a time (either of your B or C), be better for what you're trying to do here?



  • @canellas
    What do you mean by "but the layout in F is broken when the Window is displayed"?

    It will only correctly show one frame when the code you show gets executed, as that sets up to view one frame, correctly. (Or is your question about a specific bit of the code?) So that's reasonable, no?

    However, before you spend time on this, would using a QStackedWidget (https://doc.qt.io/qt-5/qstackedwidget.html), which is designed for swapping between multiple widgets so only one is shown at a time (either of your B or C), be better for what you're trying to do here?



  • @JonB ,

    First of all, thanks for your time.

    I wrapped the code in a method, and called it in the constructor of the QMainWindow. I guess I should call in another moment, right? Maybe on some show event...

    I though of QStackWidget, but that would allow the controls (QWidget) in B and C to be used, so when the user clicked on the final OK, I would not know id I should consider the data in B or C. I mean, I would like to direct the user, making him/her to explicitly choose B or C data. (dummy edit)


  • Lifetime Qt Champion

    Hi,

    Are you trying to implement some sort of wizard ? If so, then QWizard is your friend.



  • @SGaist ,

    Not really. I just wanted to show a frame B or C, according to a QRadioButton, but the way I was trying to do, the layout was messed up. @JonB was right by telling to use QStackWidget. I did not know it well, and I though the user would be able to switch between pages.
    So, thank you all for your help!



  • @JonB ,

    I marked my reply to you comment as the best, but I meant to mark your comment as the best. Do you know how to change it?



  • @canellas
    Very kind of you. Only Grand Wizards here know how to mark someone else's post as the solution, I have seen others do it in mysterious circumstances but have not graduated that far myself...


  • Lifetime Qt Champion

    AFAIK, it's a know issue of the forum software. It might have been fixed since last time I saw the report but I haven't checked recently.


Log in to reply