QLayout: Attempting to add QLayout "" to MainWindow "", which already has a layout [SOLVED]



  • I'm trying to insert a layout and I got this:

    QLayout: Attempting to add QLayout "" to MainWindow "", which already has a layout

    #include "mainwindow.hpp"
    
    #include <QVBoxLayout>
    #include <QHBoxLayout>
    #include <QLineEdit>
    #include <QPushButton>
    #include <QProgressBar>
    
    MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent)
    {
        auto layout  = new QVBoxLayout(this);
        auto vlayout = new QVBoxLayout();
        auto hlayout = new QHBoxLayout();
    
        auto edit = new QLineEdit(this);
        auto push = new QPushButton(this);
        push->setText("Go!");
    
        hlayout->addWidget(edit);
        hlayout->addWidget(push);
    
        vlayout->addLayout(hlayout);
    
        auto progress = new QProgressBar(this);
    
        vlayout->addWidget(progress);
    
        layout->addLayout(vlayout);
    
        setTabOrder(edit, push);
    }

  • Lifetime Qt Champion

    Hi,

    You can't, QMainWindow already has a special layout that handles everything in it (docks, menus, toolbars etc.) You have to put your layout on a widget that you will set on your QMainWindow through setCentralWidget



  • @SGaist I added this after the setTabOrder, is it right? - and worked!

        auto central = new QWidget;
        central->setLayout(layout);
    
        setCentralWidget(central);

  • Lifetime Qt Champion

    Yes, it is right indeed



Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.