From 75e8aaae5e8a0cbaa62fd4cd2a8235544f974a73 Mon Sep 17 00:00:00 2001 From: Stian Sandvik Thomassen Date: Thu, 18 Jun 2009 14:53:03 +1000 Subject: Made QInputDialog::getText() return null QString when rejected This reverts a behavior change introduced with Qt 4.5.0 where QInputDialog::getText() returned the line edit's text when the dialog was rejected. However, the behavior since Qt 4.0 has been to return a null QString when the dialog is rejected. Task-number: 256299 Reviewed-by: Andy Shaw --- src/gui/dialogs/qinputdialog.cpp | 6 +++--- tests/auto/qinputdialog/tst_qinputdialog.cpp | 25 +++++++++++++++++++++++-- 2 files changed, 26 insertions(+), 5 deletions(-) diff --git a/src/gui/dialogs/qinputdialog.cpp b/src/gui/dialogs/qinputdialog.cpp index 7f21d3d..d94d8fe 100644 --- a/src/gui/dialogs/qinputdialog.cpp +++ b/src/gui/dialogs/qinputdialog.cpp @@ -1129,8 +1129,8 @@ void QInputDialog::done(int result) is \a parent. The dialog will be modal and uses the specified widget \a flags. - This function returns the text which has been entered in the line - edit. It will not return an empty string. + If the dialog is accepted, this function returns the text in the dialog's + line edit. If the dialog is rejected, a null QString is returned. Use this static function like this: @@ -1159,7 +1159,7 @@ QString QInputDialog::getText(QWidget *parent, const QString &title, const QStri if (ret) { return dialog.textValue(); } else { - return text; + return QString(); } } diff --git a/tests/auto/qinputdialog/tst_qinputdialog.cpp b/tests/auto/qinputdialog/tst_qinputdialog.cpp index a658aeb..7e4b828 100644 --- a/tests/auto/qinputdialog/tst_qinputdialog.cpp +++ b/tests/auto/qinputdialog/tst_qinputdialog.cpp @@ -56,6 +56,7 @@ class tst_QInputDialog : public QObject { Q_OBJECT QWidget *parent; + QDialog::DialogCode doneCode; void (*testFunc)(QInputDialog *); static void testFuncGetInteger(QInputDialog *dialog); static void testFuncGetDouble(QInputDialog *dialog); @@ -72,6 +73,7 @@ private slots: void getText(); void getItem_data(); void getItem(); + void task256299_getTextReturnNullStringOnRejected(); }; QString stripFraction(const QString &s) @@ -245,8 +247,9 @@ void tst_QInputDialog::timerEvent(QTimerEvent *event) killTimer(event->timerId()); QInputDialog *dialog = qFindChild(parent); Q_ASSERT(dialog); - testFunc(dialog); - dialog->done(QDialog::Accepted); // cause static function call to return + if (testFunc) + testFunc(dialog); + dialog->done(doneCode); // cause static function call to return } void tst_QInputDialog::getInteger_data() @@ -266,6 +269,7 @@ void tst_QInputDialog::getInteger() QFETCH(int, max); Q_ASSERT(min < max); parent = new QWidget; + doneCode = QDialog::Accepted; testFunc = &tst_QInputDialog::testFuncGetInteger; startTimer(0); bool ok = false; @@ -305,6 +309,7 @@ void tst_QInputDialog::getDouble() QFETCH(int, decimals); Q_ASSERT(min < max && decimals >= 0 && decimals <= 13); parent = new QWidget; + doneCode = QDialog::Accepted; testFunc = &tst_QInputDialog::testFuncGetDouble; startTimer(0); bool ok = false; @@ -322,6 +327,7 @@ void tst_QInputDialog::getDouble() void tst_QInputDialog::task255502getDouble() { parent = new QWidget; + doneCode = QDialog::Accepted; testFunc = &tst_QInputDialog::testFuncGetDouble; startTimer(0); bool ok = false; @@ -347,6 +353,7 @@ void tst_QInputDialog::getText() { QFETCH(QString, text); parent = new QWidget; + doneCode = QDialog::Accepted; testFunc = &tst_QInputDialog::testFuncGetText; startTimer(0); bool ok = false; @@ -356,6 +363,19 @@ void tst_QInputDialog::getText() delete parent; } +void tst_QInputDialog::task256299_getTextReturnNullStringOnRejected() +{ + parent = new QWidget; + doneCode = QDialog::Rejected; + testFunc = 0; + startTimer(0); + bool ok = true; + const QString result = QInputDialog::getText(parent, "", "", QLineEdit::Normal, "foobar", &ok); + QVERIFY(!ok); + QVERIFY(result.isNull()); + delete parent; +} + void tst_QInputDialog::getItem_data() { QTest::addColumn("items"); @@ -373,6 +393,7 @@ void tst_QInputDialog::getItem() QFETCH(QStringList, items); QFETCH(bool, editable); parent = new QWidget; + doneCode = QDialog::Accepted; testFunc = &tst_QInputDialog::testFuncGetItem; startTimer(0); bool ok = false; -- cgit v0.12