QWidget::setLayout: Attempting to set QLayout "" on xxx "", which already has a layout

 

QLayout is a Qt application development in a very important component, however, usually are not careful when using the console have often found similar to the following warning:

QWidget::setLayout: Attempting to set QLayout "" on xxx "", which already has a layout

 

Online explanation in this regard has been a bit confusing, but also copy to copy, mainly concentrated in the QMainWindow problem. But in fact, if not careful, any QWidget use on QLayout will appear this warning. In fact, there are two general reasons:

(1) have been explicitly set QLayout again after a QWidget disposed QLayout . This case is two calls to the QWidget the setLayout method. The solution is to optimize the code or delete one QLayout .

(2) is provided implicitly QLayout again after a QWidget disposed QLayout . This situation is how to generate it? Look at the code below:

 

FootageListDock::FootageListDock(QWidget* parent)
    : QDockWidget(parent) { 
    setObjectName("footagelistDock");
    setWindowTitle(tr("Footage List")); 
    setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Expanding);
     
    QHBoxLayout* mainLayout = new QHBoxLayout(this);  // 1
    mFootageList = new FootageListWidget(this);      // 2     
   mFootageList->setRowCount(3);
mFootageList->setColumnCount(1); mFootageList->setItem(0,0,new QTableWidgetItem("Jan")); mFootageList->setItem(1,0,new QTableWidgetItem("Feb")); mFootageList->setItem(2,0,new QTableWidgetItem("Mar")); mFootageList->setRowCount(mFootageList->rowCount() + 1); mFootageList->setItem(3,0, new QTableWidgetItem("April")); mainLayout->addWidget(mFootageList); QWidget* centralWidget = new QWidget(this); centralWidget->setLayout(mainLayout); // 3 setWidget(centralWidget); }

 

Wherein two codes 1 and 2 by this pointer is set QLayout owning component, i.e. this is provided implicitly QWidget layout. 3 and at the rear of the code again explicitly call setLayout () method, which corresponds repeated provided. Then there was the warning above. The workaround is to delete 1/2 at the this pointer or setLayout calls can be. No matter what type of QWidget , should pay attention to this.

 

Editor:

https://www.cnblogs.com/csuftzzk/p/qlayout_warnings.html

 

Guess you like

Origin www.cnblogs.com/zoneofmine/p/11413305.html