From 146658a10f290603470b800d71b778239e764312 Mon Sep 17 00:00:00 2001 From: Jan Arve Saether Date: Tue, 23 Apr 2013 15:04:26 +0200 Subject: Fixed QLayout::addChildLayout(QLayout *l) when l had a parent Previously if l had a parent, addChildLayout would warn and skip the reparenting, but it would still add the sub layout to the layout. This caused some inconsistencies in the hierarchy which in worst case could cause crashes. Task-number: QTBUG-30758 Change-Id: I618ec3341636b97bd71e421201b22c746dcf43e1 Reviewed-by: Paul Olav Tvete --- src/widgets/kernel/qboxlayout.cpp | 3 ++- src/widgets/kernel/qformlayout.cpp | 4 ++-- src/widgets/kernel/qgridlayout.cpp | 6 ++++-- src/widgets/kernel/qlayout.cpp | 10 ++++++++++ src/widgets/kernel/qlayout.h | 1 + 5 files changed, 19 insertions(+), 5 deletions(-) (limited to 'src') diff --git a/src/widgets/kernel/qboxlayout.cpp b/src/widgets/kernel/qboxlayout.cpp index e1a7903628..d0e7a16999 100644 --- a/src/widgets/kernel/qboxlayout.cpp +++ b/src/widgets/kernel/qboxlayout.cpp @@ -942,7 +942,8 @@ void QBoxLayout::insertSpacerItem(int index, QSpacerItem *spacerItem) void QBoxLayout::insertLayout(int index, QLayout *layout, int stretch) { Q_D(QBoxLayout); - addChildLayout(layout); + if (!adoptLayout(layout)) + return; if (index < 0) // append index = d->list.count(); QBoxLayoutItem *it = new QBoxLayoutItem(layout, stretch); diff --git a/src/widgets/kernel/qformlayout.cpp b/src/widgets/kernel/qformlayout.cpp index e2d25de537..669faac4f6 100644 --- a/src/widgets/kernel/qformlayout.cpp +++ b/src/widgets/kernel/qformlayout.cpp @@ -976,8 +976,8 @@ void QFormLayoutPrivate::setLayout(int row, QFormLayout::ItemRole role, QLayout { if (layout) { Q_Q(QFormLayout); - q->addChildLayout(layout); - setItem(row, role, layout); + if (q->adoptLayout(layout)) + setItem(row, role, layout); } } diff --git a/src/widgets/kernel/qgridlayout.cpp b/src/widgets/kernel/qgridlayout.cpp index 12049f3303..96820e3891 100644 --- a/src/widgets/kernel/qgridlayout.cpp +++ b/src/widgets/kernel/qgridlayout.cpp @@ -1505,7 +1505,8 @@ void QGridLayout::addWidget(QWidget *widget, int fromRow, int fromColumn, void QGridLayout::addLayout(QLayout *layout, int row, int column, Qt::Alignment alignment) { Q_D(QGridLayout); - addChildLayout(layout); + if (!adoptLayout(layout)) + return; QGridBox *b = new QGridBox(layout); b->setAlignment(alignment); d->add(b, row, column); @@ -1524,7 +1525,8 @@ void QGridLayout::addLayout(QLayout *layout, int row, int column, int rowSpan, int columnSpan, Qt::Alignment alignment) { Q_D(QGridLayout); - addChildLayout(layout); + if (!adoptLayout(layout)) + return; QGridBox *b = new QGridBox(layout); b->setAlignment(alignment); d->add(b, row, (rowSpan < 0) ? -1 : row + rowSpan - 1, column, (columnSpan < 0) ? -1 : column + columnSpan - 1); diff --git a/src/widgets/kernel/qlayout.cpp b/src/widgets/kernel/qlayout.cpp index d59a9db75d..0402f9939a 100644 --- a/src/widgets/kernel/qlayout.cpp +++ b/src/widgets/kernel/qlayout.cpp @@ -806,6 +806,16 @@ void QLayout::addChildLayout(QLayout *l) } +/*! + \internal + */ +bool QLayout::adoptLayout(QLayout *layout) +{ + const bool ok = !layout->parent(); + addChildLayout(layout); + return ok; +} + #ifdef QT_DEBUG static bool layoutDebug() { diff --git a/src/widgets/kernel/qlayout.h b/src/widgets/kernel/qlayout.h index c293939bd3..6f43c2b28a 100644 --- a/src/widgets/kernel/qlayout.h +++ b/src/widgets/kernel/qlayout.h @@ -148,6 +148,7 @@ protected: void childEvent(QChildEvent *e); void addChildLayout(QLayout *l); void addChildWidget(QWidget *w); + bool adoptLayout(QLayout *layout); QRect alignmentRect(const QRect&) const; protected: -- cgit v1.2.3