From b9438e6cbdc9768253c7c696e4d9f085c067fe31 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Thorbj=C3=B8rn=20Martsum?= Date: Thu, 2 Jul 2015 07:39:32 +0200 Subject: [PATCH] QComboBox::setView only delete the old view if it is a child We have ownership for a reason - and there seems to be no good reason not to accept it here. [ChangeLog][QtWidgets][QComboBox] QComboBox::setView no longer deletes the old view directly. It now checks the ownership first. Change-Id: Icb5e5c0a6e9dc93c1d1c1a90ff57fbcc0786aa60 Reviewed-by: Gabriel de Dietrich --- src/widgets/widgets/qcombobox.cpp | 3 ++- .../widgets/qcombobox/tst_qcombobox.cpp | 23 +++++++++++++++++++ 2 files changed, 25 insertions(+), 1 deletion(-) diff --git a/src/widgets/widgets/qcombobox.cpp b/src/widgets/widgets/qcombobox.cpp index 0ecbb8a435..232ba6698e 100644 --- a/src/widgets/widgets/qcombobox.cpp +++ b/src/widgets/widgets/qcombobox.cpp @@ -546,7 +546,8 @@ void QComboBoxPrivateContainer::setItemView(QAbstractItemView *itemView) disconnect(view, SIGNAL(destroyed()), this, SLOT(viewDestroyed())); - delete view; + if (isAncestorOf(view)) + delete view; view = 0; } diff --git a/tests/auto/widgets/widgets/qcombobox/tst_qcombobox.cpp b/tests/auto/widgets/widgets/qcombobox/tst_qcombobox.cpp index c409698ec0..3835981725 100644 --- a/tests/auto/widgets/widgets/qcombobox/tst_qcombobox.cpp +++ b/tests/auto/widgets/widgets/qcombobox/tst_qcombobox.cpp @@ -164,6 +164,7 @@ private slots: void keyboardSelection(); void setCustomModelAndView(); void updateDelegateOnEditableChange(); + void respectChangedOwnershipOfItemView(); }; class MyAbstractItemDelegate : public QAbstractItemDelegate @@ -3169,5 +3170,27 @@ void tst_QComboBox::updateDelegateOnEditableChange() } } +void tst_QComboBox::respectChangedOwnershipOfItemView() +{ + QComboBox box1; + QComboBox box2; + QTableView *v1 = new QTableView; + box1.setView(v1); + + QSignalSpy spy1(v1, SIGNAL(destroyed())); + box2.setView(v1); // Ownership should now be transferred to box2 + + + QTableView *v2 = new QTableView(&box1); + box1.setView(v2); // Here we do not expect v1 to be deleted + QApplication::processEvents(); + QCOMPARE(spy1.count(), 0); + + QSignalSpy spy2(v2, SIGNAL(destroyed())); + box1.setView(v1); + QCOMPARE(spy2.count(), 1); +} + + QTEST_MAIN(tst_QComboBox) #include "tst_qcombobox.moc"