From 856efef9bf87e0b7e5ed6ad2ccb225e294321888 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan-Arve=20S=C3=A6ther?= Date: Mon, 19 Oct 2009 09:39:30 +0200 Subject: Do not loop forever if we are adding the layout itself to the layout. Reviewed-by: alexis --- src/gui/graphicsview/qgraphicsgridlayout.cpp | 4 ++++ src/gui/graphicsview/qgraphicslinearlayout.cpp | 4 ++++ tests/auto/qgraphicsgridlayout/tst_qgraphicsgridlayout.cpp | 11 +++++++++++ .../auto/qgraphicslinearlayout/tst_qgraphicslinearlayout.cpp | 11 +++++++++++ 4 files changed, 30 insertions(+) diff --git a/src/gui/graphicsview/qgraphicsgridlayout.cpp b/src/gui/graphicsview/qgraphicsgridlayout.cpp index eef4e45..9a8dba0 100644 --- a/src/gui/graphicsview/qgraphicsgridlayout.cpp +++ b/src/gui/graphicsview/qgraphicsgridlayout.cpp @@ -154,6 +154,10 @@ void QGraphicsGridLayout::addItem(QGraphicsLayoutItem *item, int row, int column qWarning("QGraphicsGridLayout::addItem: cannot add null item"); return; } + if (item == this) { + qWarning("QGraphicsGridLayout::addItem: cannot insert itself"); + return; + } d->addChildLayoutItem(item); diff --git a/src/gui/graphicsview/qgraphicslinearlayout.cpp b/src/gui/graphicsview/qgraphicslinearlayout.cpp index 0aa68df..7ff7c9b 100644 --- a/src/gui/graphicsview/qgraphicslinearlayout.cpp +++ b/src/gui/graphicsview/qgraphicslinearlayout.cpp @@ -272,6 +272,10 @@ void QGraphicsLinearLayout::insertItem(int index, QGraphicsLayoutItem *item) qWarning("QGraphicsLinearLayout::insertItem: cannot insert null item"); return; } + if (item == this) { + qWarning("QGraphicsLinearLayout::insertItem: cannot insert itself"); + return; + } d->addChildLayoutItem(item); Q_ASSERT(item); diff --git a/tests/auto/qgraphicsgridlayout/tst_qgraphicsgridlayout.cpp b/tests/auto/qgraphicsgridlayout/tst_qgraphicsgridlayout.cpp index 554292f..e33c7b6 100644 --- a/tests/auto/qgraphicsgridlayout/tst_qgraphicsgridlayout.cpp +++ b/tests/auto/qgraphicsgridlayout/tst_qgraphicsgridlayout.cpp @@ -104,6 +104,7 @@ private slots: void defaultStretchFactors(); void geometries_data(); void geometries(); + void avoidRecursionInInsertItem(); void task236367_maxSizeHint(); }; @@ -2081,6 +2082,16 @@ void tst_QGraphicsGridLayout::geometries() delete widget; } +void tst_QGraphicsGridLayout::avoidRecursionInInsertItem() +{ + QGraphicsWidget window(0, Qt::Window); + QGraphicsGridLayout *layout = new QGraphicsGridLayout(&window); + QCOMPARE(layout->count(), 0); + QTest::ignoreMessage(QtWarningMsg, "QGraphicsGridLayout::addItem: cannot insert itself"); + layout->addItem(layout, 0, 0); + QCOMPARE(layout->count(), 0); +} + void tst_QGraphicsGridLayout::task236367_maxSizeHint() { QGraphicsWidget *widget = new QGraphicsWidget; diff --git a/tests/auto/qgraphicslinearlayout/tst_qgraphicslinearlayout.cpp b/tests/auto/qgraphicslinearlayout/tst_qgraphicslinearlayout.cpp index 8e7bb45..546f92d 100644 --- a/tests/auto/qgraphicslinearlayout/tst_qgraphicslinearlayout.cpp +++ b/tests/auto/qgraphicslinearlayout/tst_qgraphicslinearlayout.cpp @@ -101,6 +101,7 @@ private slots: void updateGeometry(); void layoutDirection(); void removeLayout(); + void avoidRecursionInInsertItem(); // Task specific tests void task218400_insertStretchCrash(); @@ -1432,6 +1433,16 @@ void tst_QGraphicsLinearLayout::removeLayout() QCOMPARE(pushButton->geometry(), r2); } +void tst_QGraphicsLinearLayout::avoidRecursionInInsertItem() +{ + QGraphicsWidget window(0, Qt::Window); + QGraphicsLinearLayout *layout = new QGraphicsLinearLayout(&window); + QCOMPARE(layout->count(), 0); + QTest::ignoreMessage(QtWarningMsg, "QGraphicsLinearLayout::insertItem: cannot insert itself"); + layout->insertItem(0, layout); + QCOMPARE(layout->count(), 0); +} + void tst_QGraphicsLinearLayout::task218400_insertStretchCrash() { QGraphicsScene *scene = new QGraphicsScene; -- cgit v0.12