c++qtqlayoutqgroupbox

Add GroupBox in a layout


I am using Qt 5.15.3, with Qt cretor.

I would like to add a QGroupBox in a QVBoxLayout

Does anyone have an idea to add it in the layout ?

Here is what I have tried :

#include <QApplication>
#include <QWidget>
#include <QVBoxLayout>
#include <QCheckBox>
#include <QGroupBox>

QVBoxLayout *layoutOption = new QVBoxLayout;
QCheckBox *checkBox = new QCheckBox("Some text");

layoutOption->addWidget(checkbox);

QGroupBox *optionGB = new QGroupBox("Options");
optionGB->setLayout(layoutOption);


//trying to add optionGB in globalLayout
QVBoxLayout *globalLayout = new QVBoxLayout;
globalLayout->addLayout(optionGB);

And the issue is :

error: cannot initialize a parameter of type 'QLayout *' with an lvalue of type 'QGroupBox *'

Solution

  • I would try this way:

    //trying to add optionGB in globalLayout
    QVBoxLayout *globalLayout = new QVBoxLayout;
    globalLayout->addWidget(optionGB);