Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QLayout: Attempting to add QLayout "" to MainWindow "", which already has a layout [SOLVED]
QtWS25 Last Chance

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

Scheduled Pinned Locked Moved General and Desktop
qlayoutqmainwindowmainwindowlayout
4 Posts 2 Posters 11.6k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • Robey MardonR Offline
    Robey MardonR Offline
    Robey Mardon
    wrote on last edited by Robey Mardon
    #1

    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);
    }
    
    1 Reply Last reply
    1
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      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

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      Robey MardonR 1 Reply Last reply
      2
      • SGaistS SGaist

        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

        Robey MardonR Offline
        Robey MardonR Offline
        Robey Mardon
        wrote on last edited by Robey Mardon
        #3

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

            auto central = new QWidget;
            central->setLayout(layout);
        
            setCentralWidget(central);
        
        1 Reply Last reply
        1
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

          Yes, it is right indeed

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          2

          • Login

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • Users
          • Groups
          • Search
          • Get Qt Extensions
          • Unsolved