QLayout::addChildLayout: layout already has a parent!

qt_4_31

While working with layouts in Qt creator, I ran into an interesting problem. I am trying to add my “gridLayout_2” (the box with 9 push buttons) into the “myGrid” (the box with 8 text labels). When I run this code:

#include "widget.h"
#include "ui_widget.h"

Widget::Widget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Widget)
{
    ui->setupUi(this);

    QGridLayout * myGrid = new QGridLayout(this);


    myGrid->addWidget(ui->label,0,0);
    myGrid->addWidget(ui->label_2,0,1);
    myGrid->addWidget(ui->label_3,0,2);
    myGrid->addWidget(ui->label_4,1,0);
    myGrid->addWidget(ui->label_5,1,1);
    myGrid->addWidget(ui->label_6,1,2);
    myGrid->addWidget(ui->label_7,2,0);
    myGrid->addWidget(ui->label_8,2,1);
    myGrid->addLayout(ui->gridLayout_2,2,2);
    setLayout(myGrid);

}

Widget::~Widget()
{
    delete ui;
}

I get this error:

QLayout::addChildLayout: layout “gridLayout_2” already has a parent

And the “gridLayout_2” is not added to the “myGrid” layout. At first I thought I was doing something wrong (maybe I still am), but I found that this is a known bug in Qt Creator:

https://bugreports.qt.io/browse/QTBUG-24420

QBoxLayout::addLayout() and QGridLayout::addLayout() functions should reparent the added layout automatically if it already has a parent. Currently these functions call QLayout::addChildLayout() which does not add the child layout if it already has been added to some other layout i.e. has a parent.

Please note that QBoxLayout::addWidget() and QGridLayout::addWidget() reparent the added widget. Thus there is an inconsistency on the functionality.

So I didn’t figure out a way to add a grid layout inside one of the grids of another grid layout. If you figure this out, then be sure to let me know!

Linux – keep it simple.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s