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
17 Posts 7 Posters 35.4k 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.
  • H Offline
    H Offline
    houmingc
    wrote on last edited by houmingc
    #1

    In Mainwindow, i create below code.
    when run, i get QLayout: attempting to add QLayout "" to MainWindow, which already has a layout. How to remedy?
    i could w.setCentralWidget (centralWidget) in Main but not MainWindow.cpp

         Layout_page1= new QVBoxLayout();
         Layout_page1->addLayout(topLayout);
         Layout_page1->addWidget(ml);
    
           w_page1 = new QWidget;
          stackedLayout = new QStackedLayout;
          stackedLayout->addWidget(w_page1);
    
    K 1 Reply Last reply
    1
    • H houmingc

      In Mainwindow, i create below code.
      when run, i get QLayout: attempting to add QLayout "" to MainWindow, which already has a layout. How to remedy?
      i could w.setCentralWidget (centralWidget) in Main but not MainWindow.cpp

           Layout_page1= new QVBoxLayout();
           Layout_page1->addLayout(topLayout);
           Layout_page1->addWidget(ml);
      
             w_page1 = new QWidget;
            stackedLayout = new QStackedLayout;
            stackedLayout->addWidget(w_page1);
      
      K Offline
      K Offline
      koahnig
      wrote on last edited by
      #2

      @houmingc said:
      What is topLayout?
      Is it possibly incompatible?

      Vote the answer(s) that helped you to solve your issue(s)

      1 Reply Last reply
      0
      • SGaistS Offline
        SGaistS Offline
        SGaist
        Lifetime Qt Champion
        wrote on last edited by
        #3

        Hi,

        If MainWindow is a QMainWindow and you build centralWidget in MainWindow just call setCentralWidget(centralWidget);

        You can't set a layout on a QMainWindow because it already has one that makes all the work for dock widgets etc.

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

        H 1 Reply Last reply
        1
        • SGaistS SGaist

          Hi,

          If MainWindow is a QMainWindow and you build centralWidget in MainWindow just call setCentralWidget(centralWidget);

          You can't set a layout on a QMainWindow because it already has one that makes all the work for dock widgets etc.

          H Offline
          H Offline
          houmingc
          wrote on last edited by
          #4

          In mainWindow, i have to transverse between 5 pages using a timer.
          QWidget do not have method setCentralWidget();

          QWidget* w_page1;
          QWidget* w_page2;
          QWidget* w_page3;
          QWidget* w_page4;
          QWidget* w_page5;
          
            w_page1->setLayout(Layout_page1);
             w_page1->showMaximized();
             w_page1->setStyleSheet("background-color:black");
             w_page2->setLayout(Layout_page2);
             w_page2->showMaximized();
             w_page2->setStyleSheet("background-color:black");
          
             w_page3->setLayout(Layout_page3);
             w_page3->showMaximized();
             w_page3->setStyleSheet("background-color:white");
             w_page4->setLayout(Layout_page4);
             w_page4->showMaximized();
             w_page4->setStyleSheet("background-color:white");
          
             w_page5->setLayout(Layout_page5);
             w_page5->showMaximized();
             w_page5->setStyleSheet("background-color:white");
          
          H 1 Reply Last reply
          0
          • H houmingc

            In mainWindow, i have to transverse between 5 pages using a timer.
            QWidget do not have method setCentralWidget();

            QWidget* w_page1;
            QWidget* w_page2;
            QWidget* w_page3;
            QWidget* w_page4;
            QWidget* w_page5;
            
              w_page1->setLayout(Layout_page1);
               w_page1->showMaximized();
               w_page1->setStyleSheet("background-color:black");
               w_page2->setLayout(Layout_page2);
               w_page2->showMaximized();
               w_page2->setStyleSheet("background-color:black");
            
               w_page3->setLayout(Layout_page3);
               w_page3->showMaximized();
               w_page3->setStyleSheet("background-color:white");
               w_page4->setLayout(Layout_page4);
               w_page4->showMaximized();
               w_page4->setStyleSheet("background-color:white");
            
               w_page5->setLayout(Layout_page5);
               w_page5->showMaximized();
               w_page5->setStyleSheet("background-color:white");
            
            H Offline
            H Offline
            houmingc
            wrote on last edited by
            #5

            Layout_page3 = new QHBoxLayout(this);
            Layout_page3->addWidget(w);
            Layout_page3->addLayout(rightLayout2);
            this->setCentralWidget(Layout_page3);

            Error
            No matching function for call to
            MainWindow::setCentralWidget(QHBoxLayout*)

            1 Reply Last reply
            0
            • H Offline
              H Offline
              houmingc
              wrote on last edited by
              #6

              The code is in github
              https://github.com/houmingc/HorizontalLayout1/blob/master/HorizontalLayout/mainwindow.cpp

              1 Reply Last reply
              0
              • SGaistS Offline
                SGaistS Offline
                SGaist
                Lifetime Qt Champion
                wrote on last edited by
                #7

                setCentralWidget as the name implies needs a QWidget not a QHBoxLayout.
                Put your Layout_page3 on a placeholder widget and then set it as central widget

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

                H 1 Reply Last reply
                0
                • SGaistS SGaist

                  setCentralWidget as the name implies needs a QWidget not a QHBoxLayout.
                  Put your Layout_page3 on a placeholder widget and then set it as central widget

                  H Offline
                  H Offline
                  houmingc
                  wrote on last edited by
                  #8

                  Layout_page3 = new QHBoxLayout(this);
                  Layout_page3->addWidget(w);
                  Layout_page3->addLayout(rightLayout2);
                  setCentralWidget(Layout_page3);

                  This doesn't work, neither is this
                  this->setCentralWidget(Layout_page3);

                  1 Reply Last reply
                  0
                  • SGaistS Offline
                    SGaistS Offline
                    SGaist
                    Lifetime Qt Champion
                    wrote on last edited by
                    #9

                    Again: setCentralWidget takes a QWidget not a QLayout as a parameter.

                    Again 2:Put your Layout_page3 layout on a QWidget and then set that widget using setCentralWidget.

                    QWidget *placeholderWidget = new QWidget;
                    placeholderWidget->setLayout(Layout_page3);
                    setCentralWidget(placeholderWidget);
                    

                    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
                    1
                    • H Offline
                      H Offline
                      houmingc
                      wrote on last edited by
                      #10

                      git push my code. Thanks, although i got no error, but the warning is still there.

                      1 Reply Last reply
                      0
                      • SGaistS Offline
                        SGaistS Offline
                        SGaist
                        Lifetime Qt Champion
                        wrote on last edited by
                        #11

                        Are you still trying to set a layout on your main window ?

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

                        H 1 Reply Last reply
                        0
                        • SGaistS SGaist

                          Are you still trying to set a layout on your main window ?

                          H Offline
                          H Offline
                          houmingc
                          wrote on last edited by houmingc
                          #12

                          @SGaist
                          yes sir

                          http://picpaste.com/019a8b1d437063802a66d05904e663a6.jpg

                          1 Reply Last reply
                          0
                          • SGaistS Offline
                            SGaistS Offline
                            SGaist
                            Lifetime Qt Champion
                            wrote on last edited by
                            #13

                            So again: you can not do that as the error message says. As already stated: QMainWindow already has a layout. The one that handles dock widgets, menu bar, status bar etc. You can't replace it. The central content of a QMainWindow is a QWidget, so create one widget that does what you want and set it using setCentralWidget.

                            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
                            1
                            • Narasimha Reddy MVN Offline
                              Narasimha Reddy MVN Offline
                              Narasimha Reddy MV
                              wrote on last edited by Narasimha Reddy MV
                              #14

                              You are Creating Layout inside a widget so Don't use (Parent Instance) 'this' pointer.

                              Layout_page3 = new QHBoxLayout;

                              try this it will work

                              1 Reply Last reply
                              0
                              • Qterry.wangQ Offline
                                Qterry.wangQ Offline
                                Qterry.wang
                                wrote on last edited by
                                #15

                                Good afternoon every one!
                                I encour this issue too, Msg is:

                                18:28:21: Debugging starts
                                QLayout: Attempting to add QLayout “” to MainWindow “MainWindow”, which already has a layout

                                Reason:
                                I used the following In QMainwindow:
                                ====error code=> QGridLayout *t_gridLayout_map = new QGridLayout(this);
                                Solution:
                                remove "this" as the following:
                                ====OK code=> QGridLayout *t_gridLayout_map = new QGridLayout();

                                That's OK!
                                have a good day!

                                Have a good day !

                                JonBJ D 2 Replies Last reply
                                1
                                • Qterry.wangQ Qterry.wang

                                  Good afternoon every one!
                                  I encour this issue too, Msg is:

                                  18:28:21: Debugging starts
                                  QLayout: Attempting to add QLayout “” to MainWindow “MainWindow”, which already has a layout

                                  Reason:
                                  I used the following In QMainwindow:
                                  ====error code=> QGridLayout *t_gridLayout_map = new QGridLayout(this);
                                  Solution:
                                  remove "this" as the following:
                                  ====OK code=> QGridLayout *t_gridLayout_map = new QGridLayout();

                                  That's OK!
                                  have a good day!

                                  JonBJ Offline
                                  JonBJ Offline
                                  JonB
                                  wrote on last edited by
                                  #16

                                  @Qterry-wang said in QLayout: attempting to add QLayout "" to MainWindow, which already has a layout (solved):

                                  QGridLayout *t_gridLayout_map = new QGridLayout();

                                  This does not address the issue. It merely creates a currently-unparented QGridLayout, it depends what you then do with that. You cannot then add it directly to this, the MainWindow, since that already has a layout.

                                  1 Reply Last reply
                                  1
                                  • Qterry.wangQ Qterry.wang

                                    Good afternoon every one!
                                    I encour this issue too, Msg is:

                                    18:28:21: Debugging starts
                                    QLayout: Attempting to add QLayout “” to MainWindow “MainWindow”, which already has a layout

                                    Reason:
                                    I used the following In QMainwindow:
                                    ====error code=> QGridLayout *t_gridLayout_map = new QGridLayout(this);
                                    Solution:
                                    remove "this" as the following:
                                    ====OK code=> QGridLayout *t_gridLayout_map = new QGridLayout();

                                    That's OK!
                                    have a good day!

                                    D Offline
                                    D Offline
                                    Diaxion
                                    wrote on last edited by
                                    #17

                                    @Qterry-wang wow I was also getting similar warning and i deleted parameter like you, warning was gone. THX !

                                    1 Reply Last reply
                                    0

                                    • Login

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