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

Button not collinear with QGroupBox



  • Hi,

    I have the following GUI, consisting of some groupboxes and buttons. With the help of the radio buttons i can switsch between a small view and a big view, where the right groupbox will be set invisible. When I run the application with the big view, everything seems okay:

    14b81df1-af52-49b4-a031-79f3efd731b3-image.png

    But when I click on "small" radio button, the green button sticks out, although it should end exactly where the large outer GroupBox ends. Instead, the window is stretched a little to the right

    1b2fea24-b219-4254-9f08-46802ab295f2-image.png

    When I remove the green button, everything seems okay (the window is not stretched anymore to the right, so the problems seems to be in the size policy of the green button.

    6ca209b1-ffff-4137-bd58-52160e05dcd3-image.png

    Propertys of the green Button:

    9e1ee129-657e-4552-bbac-62a552302aa5-image.png

    And here is my function to hide/make visible the elements:

    void Gui::hideSecondOption(bool hide)
    {
        if(hide)
        {
            ui->groupBox_secondOption->setVisible(false);
    
            //A delay is necessary because when a user selects the 'Small' radio button again
            //the window will otherwise be larger than originally
            QTimer::singleShot(50, [this] {
                resize(500, 608);
            });
        } else{
            QTimer::singleShot(50, [this] {
                resize(980, 608);
            });
            ui->groupBox_secondOption->setVisible(true);
        }
    }
    


  • @TUStudi

    Is there something right to your groupBox in small layout? Show your ui design / layouts please. It seems that there is still some content, when hiding the second page. So the green button will align to this.

    Btw: I still dont see why you need these timers :)
    The same RadioButton can not get activated twice. You only can toggle between single options of a button group.
    But this has probably nothing to do with your layout issue



  • @Pl45m4 : Hi.That's a good point, maybe the button is aligned to the "QStackedWidget"? My Ui-elements look like this:

    7b405ea8-eb33-4ddc-9e0f-ec0a1d6b5fbc-image.png

    Regarding timers: I had an issue, where the original window size was not retained after reselection, and the timers fixed the issue.



  • @TUStudi

    Try a cascading horizontal + vertical layout for your page. Could be an issue with the stretch of the last column, where your green button lives or the QGridLayout in general.

    Edit:
    Yeah, that's probably it.
    Gridlayouts can have "empty" cells... Your two groupBoxes with your options take two cells above the button, which takes one, but expands to full size. As soon as you hide the content (grpBox 2nd opt) of second column, the gridLayout only has three rows (grpBx radio, grpBx first, green btn), but the QGridLayout won't delete its second column, but will leave it just empty and this takes some (little) space. Probably exact that amount of space, the green button expands to the right (aligning to the outter border of your grid).

    So try horizontal + vertical layouts or check, if there really is some empty cell (which should be, since you don't take the widget but just hide it).

    QGridLayout::columnCount() should return (at least) 2 when your app is in "small" mode (and also 2 or 3 when you switch to "big")