From 2b7edd053404f4819abf0203bb9c762799849638 Mon Sep 17 00:00:00 2001 From: Jan Arve Saether Date: Fri, 3 May 2019 15:47:57 +0200 Subject: [PATCH] Fix bug with QLayout::replaceWidget(a, a) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit It should effectively leave the layout untouched Fixes: QTBUG-69706 Change-Id: I4d8232895bf1d1ae0d1b73156ef6ec6001d8968a Reviewed-by: Thorbjørn Lund Martsum --- src/widgets/kernel/qlayout.cpp | 2 ++ tests/auto/widgets/kernel/qboxlayout/tst_qboxlayout.cpp | 4 ++++ 2 files changed, 6 insertions(+) diff --git a/src/widgets/kernel/qlayout.cpp b/src/widgets/kernel/qlayout.cpp index e3f6a56875..8ea93f7d4e 100644 --- a/src/widgets/kernel/qlayout.cpp +++ b/src/widgets/kernel/qlayout.cpp @@ -1156,6 +1156,8 @@ QLayoutItem *QLayout::replaceWidget(QWidget *from, QWidget *to, Qt::FindChildOpt Q_D(QLayout); if (!from || !to) return 0; + if (from == to) // Do not return a QLayoutItem for \a from, since ownership still + return nullptr; // belongs to the layout (since nothing was changed) int index = -1; QLayoutItem *item = 0; diff --git a/tests/auto/widgets/kernel/qboxlayout/tst_qboxlayout.cpp b/tests/auto/widgets/kernel/qboxlayout/tst_qboxlayout.cpp index fa9769a002..8dd9d7c428 100644 --- a/tests/auto/widgets/kernel/qboxlayout/tst_qboxlayout.cpp +++ b/tests/auto/widgets/kernel/qboxlayout/tst_qboxlayout.cpp @@ -571,6 +571,10 @@ void tst_QBoxLayout::replaceWidget() QCOMPARE(boxLayout->indexOf(replaceFrom), 1); QCOMPARE(boxLayout->indexOf(replaceTo), -1); + QCOMPARE(boxLayout->count(), 3); + boxLayout->replaceWidget(replaceFrom, replaceFrom); + QCOMPARE(boxLayout->count(), 3); + delete boxLayout->replaceWidget(replaceFrom, replaceTo); QCOMPARE(boxLayout->indexOf(replaceFrom), -1);