Congratulations to our 2022 Qt Champions!

[SOLVED] Help me with creating a Groupbox with listwidget and buttons!

  • Hello. I want to create a groupbox like this:

    With this code:
    @optionsGroupBox = new QGroupBox(tr("Items"));
    optionsGroupBoxLayout = new QGridLayout;
    optionsGroupBoxLayout->addWidget(ui->listWidget, 1, 0);
    optionsGroupBoxLayout->addWidget(ui->addButton, 1, 1);
    optionsGroupBoxLayout->addWidget(ui->editButton, 1, 1);
    optionsGroupBoxLayout->addWidget(ui->removeButton, 1, 1);
    optionsGroupBoxLayout->addWidget(ui->clearButton, 1, 1);
    optionsGroupBoxLayout->addWidget(ui->moveupButton, 1, 1);
    optionsGroupBoxLayout->addWidget(ui->movedownButton, 1, 1);

    I have this:

    All the buttons are in one place now.. I know why.. cause i have set all to 1,1.. but what else could i do? It is something like 1,1,another number here,another one here ... Don't know!

    P.S Another solution would be to make a vertical layout of the buttons and then add the layout to the groupbox .. But how?
    optionsGroupBoxLayout->addLayout(ui->verticalLayout, 1, 1);
    Will give me an error:QLayout::addChildLayout: layout "verticalLayout" already has a parent
    The program has unexpectedly finished.

    Thanks for any answer, Leon :)

  • Is there a reason you add all the widgets to the same row and column (that is: the same cell in the grid layout)?

    I would probably not use a QGridLayout, but use a QVBoxLayout for the buttons, and a QHBoxLayout to put the listbox and the layout for the buttons in.

  • Hello andre! Thanks for the tip! It worked :)

    @mainLayout2 = new QHBoxLayout;
    mainLayout3 = new QVBoxLayout;
    optionsGroupBox = new QGroupBox(tr("Items"));
    optionsGroupBoxLayout = new QGridLayout;
    optionsGroupBoxLayout->addLayout(mainLayout2, 1,0);
    optionsGroupBoxLayout->addLayout(mainLayout3, 1,1);

Log in to reply