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"