From 43619db05d55ca619dac11fdb7327b2b45507cb9 Mon Sep 17 00:00:00 2001 From: Lars Knoll Date: Tue, 20 Nov 2012 13:34:06 +0100 Subject: Emit destroyed() signal before children get deleted Make sure we always emit the destroyed() signal before we delete our children. This wasn't working correctly for QWidget based classes, as the QWidget destructor deletes all children itself. Task-number: QTBUG-24672 Change-Id: Iecdff3489196271177edfeba1c4a2c5800e255af Reviewed-by: Olivier Goffart --- tests/auto/widgets/kernel/qwidget/tst_qwidget.cpp | 100 ++++++++++++++++++++++ 1 file changed, 100 insertions(+) (limited to 'tests') diff --git a/tests/auto/widgets/kernel/qwidget/tst_qwidget.cpp b/tests/auto/widgets/kernel/qwidget/tst_qwidget.cpp index f3f63b2067..2a5e59f825 100644 --- a/tests/auto/widgets/kernel/qwidget/tst_qwidget.cpp +++ b/tests/auto/widgets/kernel/qwidget/tst_qwidget.cpp @@ -396,6 +396,8 @@ private slots: void touchEventSynthesizedMouseEvent(); void styleSheetPropagation(); + + void destroyedSignal(); private: bool ensureScreenSize(int width, int height); QWidget *testWidget; @@ -9592,6 +9594,104 @@ void tst_QWidget::styleSheetPropagation() } } +class DestroyTester : public QObject +{ + Q_OBJECT +public: + DestroyTester(QObject *parent) : QObject(parent) { parentDestroyed = 0; } + static int parentDestroyed; +public slots: + void parentDestroyedSlot() { + ++parentDestroyed; + } +}; + +int DestroyTester::parentDestroyed = 0; + +void tst_QWidget::destroyedSignal() +{ + { + QWidget *w = new QWidget; + DestroyTester *t = new DestroyTester(w); + connect(w, SIGNAL(destroyed()), t, SLOT(parentDestroyedSlot())); + QCOMPARE(DestroyTester::parentDestroyed, 0); + delete w; + QCOMPARE(DestroyTester::parentDestroyed, 1); + } + + { + QWidget *w = new QWidget; + DestroyTester *t = new DestroyTester(w); + connect(w, SIGNAL(destroyed()), t, SLOT(parentDestroyedSlot())); + w->blockSignals(true); + QCOMPARE(DestroyTester::parentDestroyed, 0); + delete w; + QCOMPARE(DestroyTester::parentDestroyed, 1); + } + + { + QObject *o = new QWidget; + DestroyTester *t = new DestroyTester(o); + connect(o, SIGNAL(destroyed()), t, SLOT(parentDestroyedSlot())); + QCOMPARE(DestroyTester::parentDestroyed, 0); + delete o; + QCOMPARE(DestroyTester::parentDestroyed, 1); + } + + { + QObject *o = new QWidget; + DestroyTester *t = new DestroyTester(o); + connect(o, SIGNAL(destroyed()), t, SLOT(parentDestroyedSlot())); + o->blockSignals(true); + QCOMPARE(DestroyTester::parentDestroyed, 0); + delete o; + QCOMPARE(DestroyTester::parentDestroyed, 1); + } + + { + QWidget *w = new QWidget; + DestroyTester *t = new DestroyTester(0); + connect(w, SIGNAL(destroyed()), t, SLOT(parentDestroyedSlot())); + QCOMPARE(DestroyTester::parentDestroyed, 0); + delete w; + QCOMPARE(DestroyTester::parentDestroyed, 1); + delete t; + } + + { + QWidget *w = new QWidget; + DestroyTester *t = new DestroyTester(0); + connect(w, SIGNAL(destroyed()), t, SLOT(parentDestroyedSlot())); + w->blockSignals(true); + QCOMPARE(DestroyTester::parentDestroyed, 0); + delete w; + QCOMPARE(DestroyTester::parentDestroyed, 1); + delete t; + } + + { + QObject *o = new QWidget; + DestroyTester *t = new DestroyTester(0); + connect(o, SIGNAL(destroyed()), t, SLOT(parentDestroyedSlot())); + QCOMPARE(DestroyTester::parentDestroyed, 0); + delete o; + QCOMPARE(DestroyTester::parentDestroyed, 1); + delete t; + } + + { + QObject *o = new QWidget; + DestroyTester *t = new DestroyTester(0); + connect(o, SIGNAL(destroyed()), t, SLOT(parentDestroyedSlot())); + o->blockSignals(true); + QCOMPARE(DestroyTester::parentDestroyed, 0); + delete o; + QCOMPARE(DestroyTester::parentDestroyed, 1); + delete t; + } + +} + #ifndef QTEST_NO_CURSOR void tst_QWidget::underMouse() { -- cgit v1.2.3