From 0c0f22ec0e36d7001c8195dcc6e390a37118e33e Mon Sep 17 00:00:00 2001 From: Michael Hasselmann Date: Mon, 7 Jun 2010 17:18:54 +0200 Subject: Fix QApplication/QWidget to really take ownership of input contexts * src/gui/kernel/[qapplication|qwidget].cpp, tests/auto/qapplication/tst_[qapplication|qwidget].cpp (setInputContext): The documentation for [QApplication|QWidget]::setInputContext claims that the [QApplication|QWidget] instance would take ownership. This commit fixes the setter to also reparent the input context. Furthermore, the crappy test for this setter was improved. Reviewed-by: Denis Dzyubenko --- src/gui/kernel/qapplication.cpp | 1 + src/gui/kernel/qwidget.cpp | 2 ++ tests/auto/qapplication/tst_qapplication.cpp | 19 +++++++++++++------ tests/auto/qwidget/tst_qwidget.cpp | 6 +++++- 4 files changed, 21 insertions(+), 7 deletions(-) diff --git a/src/gui/kernel/qapplication.cpp b/src/gui/kernel/qapplication.cpp index a2c058a..8529615 100644 --- a/src/gui/kernel/qapplication.cpp +++ b/src/gui/kernel/qapplication.cpp @@ -5297,6 +5297,7 @@ void QApplication::setInputContext(QInputContext *inputContext) } delete QApplicationPrivate::inputContext; QApplicationPrivate::inputContext = inputContext; + QApplicationPrivate::inputContext->setParent(this); } /*! diff --git a/src/gui/kernel/qwidget.cpp b/src/gui/kernel/qwidget.cpp index b5879ae..91dfaab 100644 --- a/src/gui/kernel/qwidget.cpp +++ b/src/gui/kernel/qwidget.cpp @@ -332,6 +332,8 @@ void QWidget::setInputContext(QInputContext *context) if (d->ic) delete d->ic; d->ic = context; + if (d->ic) + d->ic->setParent(this); #endif } diff --git a/tests/auto/qapplication/tst_qapplication.cpp b/tests/auto/qapplication/tst_qapplication.cpp index 43fbba1..1a38070 100644 --- a/tests/auto/qapplication/tst_qapplication.cpp +++ b/tests/auto/qapplication/tst_qapplication.cpp @@ -189,15 +189,22 @@ void tst_QApplication::getSetCheck() { int argc = 0; QApplication obj1(argc, 0, QApplication::GuiServer); - // QInputContext * QApplication::inputContext() - // void QApplication::setInputContext(QInputContext *) MyInputContext *var1 = new MyInputContext; + + // QApplication takes ownership, so check for reparenting: obj1.setInputContext(var1); - QCOMPARE((QInputContext *)var1, obj1.inputContext()); + QCOMPARE(var1->parent(), static_cast(&obj1)); + + // Test for self-assignment: + obj1.setInputContext(obj1.inputContext()); + QVERIFY(obj1.inputContext()); + QCOMPARE(static_cast(var1), obj1.inputContext()); + + // Resetting the input context to 0 is not allowed: QTest::ignoreMessage(QtWarningMsg, "QApplication::setInputContext: called with 0 input context"); - obj1.setInputContext((QInputContext *)0); - QCOMPARE((QInputContext *)var1, obj1.inputContext()); - // delete var1; // No delete, since QApplication takes ownership + obj1.setInputContext(0); + + QCOMPARE(static_cast(var1), obj1.inputContext()); } class CloseEventTestWindow : public QWidget diff --git a/tests/auto/qwidget/tst_qwidget.cpp b/tests/auto/qwidget/tst_qwidget.cpp index 5d47aed..83a9e3a 100644 --- a/tests/auto/qwidget/tst_qwidget.cpp +++ b/tests/auto/qwidget/tst_qwidget.cpp @@ -605,10 +605,14 @@ void tst_QWidget::getSetCheck() obj1.setAttribute(Qt::WA_InputMethodEnabled); obj1.setInputContext(var13); QCOMPARE(static_cast(var13), obj1.inputContext()); + // QWidget takes ownership, so check parent + QCOMPARE(var13->parent(), static_cast(&obj1)); + // Check self assignment + obj1.setInputContext(obj1.inputContext()); + QCOMPARE(static_cast(var13), obj1.inputContext()); obj1.setInputContext((QInputContext *)0); QCOMPARE(qApp->inputContext(), obj1.inputContext()); QVERIFY(qApp->inputContext() != var13); - //delete var13; // No delete, since QWidget takes ownership // bool QWidget::autoFillBackground() // void QWidget::setAutoFillBackground(bool) -- cgit v0.12