QFileDialog: Call reject() on Key_Escape even when itemview has focus

Replace hide() call (present in itemViewKeyboardEvent since Qt 4.5)
by reject(). Add signal spy to existing test function.

QDialog doc states that reject() will always be called on Key_Escape.
hide() is not enough: it makes exec() terminate and return the proper
value, but the signals finished(int) and rejected() will not be sent.

Task-number: QTBUG-7690
Change-Id: Ica4ae2843574478c5b9a7672f871f3ef3f16f3c9
Done-with: Jan Blumschein <jan@jan-blumschein.de>
Reviewed-by: Friedemann Kleint <Friedemann.Kleint@theqtcompany.com>
This commit is contained in:
Giuseppe D'Angelo 2015-12-07 18:15:07 +01:00 committed by Simon Hausmann
parent 52e994c8a3
commit c5e972eb87
2 changed files with 23 additions and 12 deletions

View File

@ -3793,7 +3793,7 @@ bool QFileDialogPrivate::itemViewKeyboardEvent(QKeyEvent *event) {
Q_Q(QFileDialog);
if (event->matches(QKeySequence::Cancel)) {
q->hide();
q->reject();
return true;
}

View File

@ -130,6 +130,7 @@ private slots:
#endif
void completionOnLevelAfterRoot();
void task233037_selectingDirectory();
void task235069_hideOnEscape_data();
void task235069_hideOnEscape();
void task203703_returnProperSeparator();
void task228844_ensurePreviousSorting();
@ -770,26 +771,36 @@ void tst_QFileDialog2::task233037_selectingDirectory()
current.rmdir("test");
}
void tst_QFileDialog2::task235069_hideOnEscape_data()
{
QTest::addColumn<QString>("childName");
QTest::newRow("listView") << QStringLiteral("listView");
QTest::newRow("fileNameEdit") << QStringLiteral("fileNameEdit");
QTest::newRow("treeView") << QStringLiteral("treeView");
}
void tst_QFileDialog2::task235069_hideOnEscape()
{
QFETCH(QString, childName);
QDir current = QDir::currentPath();
QNonNativeFileDialog fd;
QSignalSpy spyFinished(&fd, &QDialog::finished);
QVERIFY(spyFinished.isValid());
QSignalSpy spyRejected(&fd, &QDialog::rejected);
QVERIFY(spyRejected.isValid());
fd.setViewMode(QFileDialog::List);
fd.setDirectory(current.absolutePath());
fd.setAcceptMode( QFileDialog::AcceptSave);
fd.setAcceptMode(QFileDialog::AcceptSave);
fd.show();
QListView *list = fd.findChild<QListView*>("listView");
list->setFocus();
QWidget *child = fd.findChild<QWidget *>(childName);
QVERIFY(child);
child->setFocus();
QTest::qWait(200);
QTest::keyClick(list, Qt::Key_Escape);
QTest::keyClick(child, Qt::Key_Escape);
QCOMPARE(fd.isVisible(), false);
QNonNativeFileDialog fd2;
fd2.setDirectory(current.absolutePath());
fd2.setAcceptMode( QFileDialog::AcceptSave);
fd2.show();
QLineEdit *edit = fd2.findChild<QLineEdit*>("fileNameEdit");
QTest::keyClick(edit, Qt::Key_Escape);
QCOMPARE(fd2.isVisible(), false);
QCOMPARE(spyFinished.count(), 1); // QTBUG-7690
QCOMPARE(spyRejected.count(), 1); // reject(), don't hide()
}
#ifdef QT_BUILD_INTERNAL