From 05ca21411eaaf3ea5e9cc9d82ecfaa8753f9f319 Mon Sep 17 00:00:00 2001 From: David Faure Date: Thu, 22 Dec 2011 19:02:34 +0100 Subject: [PATCH] Handle -1 (Invalid Key) and Qt::Key_unknown gracefully in encodeString. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Previously they would fall into the unicode handling and return very strange values. Change-Id: I62a53894c0983bf53fd79f924b40a6fd3ba02993 Reviewed-by: Morten Johan Sørvig --- src/gui/kernel/qkeysequence.cpp | 5 +++++ .../kernel/qkeysequence/tst_qkeysequence.cpp | 20 +++++++++++++++++++ 2 files changed, 25 insertions(+) diff --git a/src/gui/kernel/qkeysequence.cpp b/src/gui/kernel/qkeysequence.cpp index 9c0b07c64eb..c17d41ba6f2 100644 --- a/src/gui/kernel/qkeysequence.cpp +++ b/src/gui/kernel/qkeysequence.cpp @@ -1342,6 +1342,11 @@ QString QKeySequencePrivate::encodeString(int key, QKeySequence::SequenceFormat { bool nativeText = (format == QKeySequence::NativeText); QString s; + + // Handle -1 (Invalid Key) and Qt::Key_unknown gracefully + if (key == -1 || key == Qt::Key_unknown) + return s; + #if defined(Q_OS_MAC) if (nativeText) { // On Mac OS X the order (by default) is Meta, Alt, Shift, Control. diff --git a/tests/auto/gui/kernel/qkeysequence/tst_qkeysequence.cpp b/tests/auto/gui/kernel/qkeysequence/tst_qkeysequence.cpp index 9845b388b42..f8eae2067ec 100644 --- a/tests/auto/gui/kernel/qkeysequence/tst_qkeysequence.cpp +++ b/tests/auto/gui/kernel/qkeysequence/tst_qkeysequence.cpp @@ -122,6 +122,8 @@ private slots: void mnemonic(); void toString_data(); void toString(); + void toStringFromKeycode_data(); + void toStringFromKeycode(); void streamOperators_data(); void streamOperators(); void parseString_data(); @@ -476,6 +478,24 @@ void tst_QKeySequence::toString() } +void tst_QKeySequence::toStringFromKeycode_data() +{ + QTest::addColumn("keycode"); + QTest::addColumn("expectedString"); + + QTest::newRow("A") << QKeySequence(Qt::Key_A) << "A"; + QTest::newRow("-1") << QKeySequence(-1) << ""; + QTest::newRow("Unknown") << QKeySequence(Qt::Key_unknown) << ""; +} + +void tst_QKeySequence::toStringFromKeycode() +{ + QFETCH(QKeySequence, keycode); + QFETCH(QString, expectedString); + + QCOMPARE(QKeySequence(keycode).toString(), expectedString); +} + void tst_QKeySequence::streamOperators_data() { operatorQString_data();