QLayout: attempting to add QLayout "" to MainWindow, which already has a layout (solved)
-
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.cppLayout_page1= new QVBoxLayout(); Layout_page1->addLayout(topLayout); Layout_page1->addWidget(ml); w_page1 = new QWidget; stackedLayout = new QStackedLayout; stackedLayout->addWidget(w_page1);
-
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.
-
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");
-
-
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 -
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);
-
Are you still trying to set a layout on your main window ?
-
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.
-
You are Creating Layout inside a widget so Don't use (Parent Instance) 'this' pointer.
Layout_page3 = new QHBoxLayout;
try this it will work
-
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 layoutReason:
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! -
@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 tothis
, theMainWindow
, since that already has a layout. -
@Qterry-wang wow I was also getting similar warning and i deleted parameter like you, warning was gone. THX !