Important: Please read the Qt Code of Conduct -

Coding objects in GroupBox in a main layout

  • I'm creating a Dialog for which I have created a main layout. I have added a 2D array of toolbuttons to the Dialog and next need to create a GroupBox that contains a few child widgets. To get the GroupBox to show up on the Dialog, I've had to add it to a SubLayout. But when I try to write code that places child widgets in the GroupBox, they show up outside the GroupBox. I have worked on this all day and have tried several variations suggested by the Qt Documentation examples. . Below is the code that I have written so far, it just attempts to add a QLabel to the GroupBox. Thanks for any help in advance.

    #include "gamedialog.h"
    #include <QtGui>

    GameDialog::GameDialog(QWidget parent) :QDialog(parent)
    // Create and configure layouts
    mainLayout = new QVBoxLayout(this);
    QGridLayout* gridLayout = new QGridLayout;
    QVBoxLayout* groupBoxLayout = new QVBoxLayout;

    // Create grid of toolbuttons and add to layout
    const int ROWS = 11;
    const int COLUMNS = 21;
    for(int i = 0; i < ROWS; i++)
            for(int j = 0; j < COLUMNS; j++)
                toolButton = new QToolButton;
                gridLayout->addWidget(toolButton, i, j);
    groupBox = new QGroupBox("Simulation Configuration");
    numStepsLabel = new QLabel("Number of Steps");


  • Well, I'm not sure what you meant, but if you want your gridLayout (containing those toolbuttons) to be inside groupBox, shouldn't you put the gridLayout inside your groupBox? Something like this:

    // Make grid
    QGridLayout *gridLayout = ...

    for(int i = 0; ...) ... gridLayout->addWidget(toolButton, i, j);

    // Make groupbox and set layout
    groupBox = new QGroupBox;
    QVBoxLayout groupBoxLayout = new QVBoxLayout;

    // Add things to groupbox (via layout)
    groupBoxLayout->addWidget(new QLabel);

    ...And then put groupBox to dialog?

    EDIT: I mean, it seems that you add your grid to main layout (line 12), and not in your groupbox's layout.

Log in to reply