c++qtqt5qvboxlayout

qt button added on menu bar


I'm trying to add buttons to a vertical layout in QT.

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
{
    ui.setupUi(this);

    mRootLayout = new QVBoxLayout(this);
    setLayout(mRootLayout);

    mRootLayout->addWidget(new QPushButton("Button1", this));
    mRootLayout->addWidget(new QPushButton("Button2", this));

}

I have 2 problems 1. The buttons are created on top of the menu bar 2. The buttons are not one under the other one.

I'm using a QVBoxLayout.

enter image description here


Solution

  • I think code must be change to:

    mRootLayout = new QVBoxLayout(ui->centralWidget);
    mRootLayout->addWidget(new QPushButton("Button1", this));
    mRootLayout->addWidget(new QPushButton("Button2", this));
    

    It's not necessary do setLayout().