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 <paul.tvete@digia.com>
This commit is contained in:
Jan Arve Saether 2013-04-23 15:04:26 +02:00 committed by The Qt Project
parent ad3b61554b
commit 146658a10f
6 changed files with 40 additions and 5 deletions

View File

@ -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);

View File

@ -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);
}
}

View File

@ -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);

View File

@ -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()
{

View File

@ -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:

View File

@ -68,6 +68,7 @@ public slots:
private slots:
void insertSpacerItem();
void insertLayout();
void sizeHint();
void sizeConstraints();
void setGeometry();
@ -166,6 +167,26 @@ void tst_QBoxLayout::insertSpacerItem()
window->show();
}
void tst_QBoxLayout::insertLayout()
{
QWidget *window = new QWidget;
QVBoxLayout *vbox = new QVBoxLayout(window);
QVBoxLayout *dummyParentLayout = new QVBoxLayout;
QHBoxLayout *subLayout = new QHBoxLayout;
dummyParentLayout->addLayout(subLayout);
QCOMPARE(subLayout->parent(), dummyParentLayout);
QCOMPARE(dummyParentLayout->count(), 1);
// add subLayout to another layout
QTest::ignoreMessage(QtWarningMsg, "QLayout::addChildLayout: layout \"\" already has a parent");
vbox->addLayout(subLayout);
QCOMPARE((subLayout->parent() == vbox), (vbox->count() == 1));
delete dummyParentLayout;
delete window;
}
void tst_QBoxLayout::sizeHint()
{
QWidget *window = new QWidget;