plannerLeftDisplay
est un parent, exécutez code suivant:
this->setObjectName("this");
QWidget* plannerLeftDisplay = new QWidget(this);
plannerLeftDisplay->setObjectName("plannerLeftDisplay");
QVBoxLayout* plannerLeftLayout = new QVBoxLayout();
plannerLeftLayout->addWidget(new QWidget());
plannerLeftDisplay->setLayout(plannerLeftLayout);
qDebug() << "parent is "<< plannerLeftLayout->parent();
La sortie sera:
parent est QWidget (0x2844a2c0, name = "plannerLeftDisplay")
Voir Qt source code :
void QWidget::setLayout(QLayout *l)
{
if (!l) {
qWarning("QWidget::setLayout: Cannot set layout to 0");
return;
}
if (layout()) {
if (layout() != l)
qWarning("QWidget::setLayout: Attempting to set QLayout \"%s\" on %s \"%s\", which already has a"
" layout", l->objectName().toLocal8Bit().data(), metaObject()->className(),
objectName().toLocal8Bit().data());
return;
}
QObject *oldParent = l->parent();
//in your case it is NULL
if (oldParent && oldParent != this) {
if (oldParent->isWidgetType()) {
// Steal the layout off a widget parent. Takes effect when
// morphing laid-out container widgets in Designer.
QWidget *oldParentWidget = static_cast<QWidget *>(oldParent);
oldParentWidget->takeLayout();
} else {
qWarning("QWidget::setLayout: Attempting to set QLayout \"%s\" on %s \"%s\", when the QLayout already has a parent",
l->objectName().toLocal8Bit().data(), metaObject()->className(),
objectName().toLocal8Bit().data());
return;
}
}
Q_D(QWidget);
l->d_func()->topLevel = true;
d->layout = l;
//reparent procedure
if (oldParent != this) { //NULL really != this
l->setParent(this); //so layout has a parent now
l->d_func()->reparentChildWidgets(this);
l->invalidate();
}
if (isWindow() && d->maybeTopData())
d->topData()->sizeAdjusted = false;
}
Il devrait en être ainsi, car quand un widget a une disposition et devrait être supprimé, vous n'avez pas besoin de supprimer la mise en page manuellement, tout sera fait automatiquement.
il sera 'null', est-ce pas? –
ne mettra pas setlayout lui donner un parent? – Nyaruko
Il semble que [vous avez raison] (http://doc.qt.io/qt-5/qwidget.html#setLayout) –