Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Hide/Show Qt Widgets depending on radio buttons: The original window size is not retained after reselection.
Forum Updated to NodeBB v4.3 + New Features

Hide/Show Qt Widgets depending on radio buttons: The original window size is not retained after reselection.

Scheduled Pinned Locked Moved Solved General and Desktop
qwidgetgui
10 Posts 3 Posters 3.6k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • T Offline
    T Offline
    TUStudi
    wrote on last edited by TUStudi
    #1

    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:

    b18e6fd2-010a-4775-94bb-fbbe5ce49a4b-image.png

    When I chosse "show 2" the second groupbox appears and the window is resized like this:
    ab518ca7-59f7-4185-a84c-a705d6ba352e-image.png

    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?

    0d5806dd-c5e0-4595-9572-00da4151ec0c-image.png

    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?

    JonBJ 1 Reply Last reply
    0
    • mrjjM Offline
      mrjjM Offline
      mrjj
      Lifetime Qt Champion
      wrote on last edited by mrjj
      #9

      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);
              });
      
          }
      }
      
      T 1 Reply Last reply
      3
      • T TUStudi

        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:

        b18e6fd2-010a-4775-94bb-fbbe5ce49a4b-image.png

        When I chosse "show 2" the second groupbox appears and the window is resized like this:
        ab518ca7-59f7-4185-a84c-a705d6ba352e-image.png

        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?

        0d5806dd-c5e0-4595-9572-00da4151ec0c-image.png

        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?

        JonBJ Online
        JonBJ Online
        JonB
        wrote on last edited by JonB
        #2

        @TUStudi
        I don't suppose it will alter, but does it help if in your hide case you do the resize() after setting invisible?

        If you leave groupBox_second permanently invisible and just do the resize()s does it then work correctly?

        Otherwise, are your boxes on a layout?

        T 1 Reply Last reply
        2
        • mrjjM Offline
          mrjjM Offline
          mrjj
          Lifetime Qt Champion
          wrote on last edited by
          #3

          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);

          1 Reply Last reply
          3
          • JonBJ JonB

            @TUStudi
            I don't suppose it will alter, but does it help if in your hide case you do the resize() after setting invisible?

            If you leave groupBox_second permanently invisible and just do the resize()s does it then work correctly?

            Otherwise, are your boxes on a layout?

            T Offline
            T Offline
            TUStudi
            wrote on last edited by TUStudi
            #4

            @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 your hide case you do the resize() after setting invisible?

            Hey, good approach, but it does not help.

            If you leave groupBox_second permanently invisible and just do the resize()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):
            9c62832d-da57-4606-939e-4c88e1213a26-image.png

            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.

            JonBJ 1 Reply Last reply
            0
            • mrjjM Offline
              mrjjM Offline
              mrjj
              Lifetime Qt Champion
              wrote on last edited by
              #5

              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 ?

              T 1 Reply Last reply
              0
              • T TUStudi

                @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 your hide case you do the resize() after setting invisible?

                Hey, good approach, but it does not help.

                If you leave groupBox_second permanently invisible and just do the resize()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):
                9c62832d-da57-4606-939e-4c88e1213a26-image.png

                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.

                JonBJ Online
                JonBJ Online
                JonB
                wrote on last edited by JonB
                #6

                @TUStudi
                @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.

                1 Reply Last reply
                0
                • mrjjM mrjj

                  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 ?

                  T Offline
                  T Offline
                  TUStudi
                  wrote on last edited by TUStudi
                  #7

                  @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.

                  15cb3060-0253-4166-a88a-dcb5474edad4-image.png

                  1 Reply Last reply
                  0
                  • mrjjM Offline
                    mrjjM Offline
                    mrjj
                    Lifetime Qt Champion
                    wrote on last edited by mrjj
                    #8

                    Hi
                    I tried to recreate the situation and i cant reproduce it.
                    it does go back to old size.

                    alt text

                    --

                    Ok. but you hide the outer group box ?
                    alt text
                    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.

                    1 Reply Last reply
                    3
                    • mrjjM Offline
                      mrjjM Offline
                      mrjj
                      Lifetime Qt Champion
                      wrote on last edited by mrjj
                      #9

                      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);
                              });
                      
                          }
                      }
                      
                      T 1 Reply Last reply
                      3
                      • mrjjM mrjj

                        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);
                                });
                        
                            }
                        }
                        
                        T Offline
                        T Offline
                        TUStudi
                        wrote on last edited by
                        #10

                        @mrjj That worked, thank you very much.

                        1 Reply Last reply
                        0

                        • Login

                        • Login or register to search.
                        • First post
                          Last post
                        0
                        • Categories
                        • Recent
                        • Tags
                        • Popular
                        • Users
                        • Groups
                        • Search
                        • Get Qt Extensions
                        • Unsolved