开发者

whats up with QLayout->setParent

When I try to change the location of a layout with setParent the result is non-functional or odd.

the following works:

ui.txDiag_1->setParent(ui.tab_2);
movingHlayout = new QVBoxLayout(ui.tab_2);
movingHlayout->setSpacing(3);
movingHlayout->setMargin(3);
movingHlayout->setObjectName(QString::fromUtf8("movingHlayout"));
movingHlayout->addWidget(ui.txDiag_1);

but this doesnt(movingHlayout has been constructed before hand):

ui.txDiag_1->setParent(ui.tab_2);
movingHlayout->setParent(ui.tab_2);
movingHlayout->setSpacing(3);
movingHlayout-&g开发者_JS百科t;setMargin(3);
movingHlayout->setObjectName(QString::fromUtf8("movingHlayout"));
movingHlayout->addWidget(ui.txDiag_1);


You possibly have to call setLayout() on the widget you are setting the layout up in.

http://doc.qt.io/qt-5/qwidget.html#setLayout

As you can see from the docs, the ownership of the layout will automatically be set to the target widget.


To complete the other answer, here is the reason why setParent does not work as you expect:

setParent is not in QLayout implementation, but in QObject only. So using it will only change the pointer ownership and deletion, not the widget layout mechanism.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜