aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorSami Shalayel <sami.shalayel@qt.io>2023-06-05 14:54:28 +0200
committerQt Cherry-pick Bot <cherrypick_bot@qt-project.org>2023-06-07 16:39:01 +0000
commit3c7812ce24c1d5fea84a750d3b6b6d89c711aec6 (patch)
tree0ed9b6f66597c6e176d9e50718cf5ee05b393793
parent5c434910ddffe834aa530a2741bfeed24a1d012a (diff)
QQuickLabsPlatformMenuItem: accept KeySequence in shortcut
The documentation claims that the shortcut of a menuitem can be bound to a QKeySequence (or at least the qml version of it) but menuitem actually only supports int or strings (that are wrapped into a QVariant). Implement the missing codepath for passing QKeySequence in the QVariant for C++ users, instead of constructing a garbage QKeySequence from the string obtained by QKeySequence::toString(). Fixes: QTBUG-113743 Change-Id: I6f4c659a79a4c23de34be92770815d9e14a6a554 Reviewed-by: Oliver Eftevaag <oliver.eftevaag@qt.io> (cherry picked from commit a4de7389bf90ab62c5d12391f9bb0d03da9b4b82) Reviewed-by: Qt Cherry-pick Bot <cherrypick_bot@qt-project.org>
-rw-r--r--src/labs/platform/qquicklabsplatformmenuitem.cpp6
-rw-r--r--tests/auto/quickcontrols/platform/data/tst_menuitem.qml25
-rw-r--r--tests/auto/quickcontrols/platform/tst_platform.cpp10
3 files changed, 41 insertions, 0 deletions
diff --git a/src/labs/platform/qquicklabsplatformmenuitem.cpp b/src/labs/platform/qquicklabsplatformmenuitem.cpp
index 31e91add36..ab45831b39 100644
--- a/src/labs/platform/qquicklabsplatformmenuitem.cpp
+++ b/src/labs/platform/qquicklabsplatformmenuitem.cpp
@@ -152,6 +152,8 @@ void QQuickLabsPlatformMenuItem::sync()
QKeySequence sequence;
if (m_shortcut.metaType().id() == QMetaType::Int)
sequence = QKeySequence(static_cast<QKeySequence::StandardKey>(m_shortcut.toInt()));
+ else if (m_shortcut.metaType().id() == QMetaType::QKeySequence)
+ sequence = m_shortcut.value<QKeySequence>();
else
sequence = QKeySequence::fromString(m_shortcut.toString());
m_handle->setShortcut(sequence.toString());
@@ -586,6 +588,8 @@ void QQuickLabsPlatformMenuItem::addShortcut()
QKeySequence sequence;
if (m_shortcut.metaType().id() == QMetaType::Int)
sequence = QKeySequence(static_cast<QKeySequence::StandardKey>(m_shortcut.toInt()));
+ else if (m_shortcut.metaType().id() == QMetaType::QKeySequence)
+ sequence = m_shortcut.value<QKeySequence>();
else
sequence = QKeySequence::fromString(m_shortcut.toString());
if (!sequence.isEmpty() && m_enabled) {
@@ -606,6 +610,8 @@ void QQuickLabsPlatformMenuItem::removeShortcut()
QKeySequence sequence;
if (m_shortcut.metaType().id() == QMetaType::Int)
sequence = QKeySequence(static_cast<QKeySequence::StandardKey>(m_shortcut.toInt()));
+ else if (m_shortcut.metaType().id() == QMetaType::QKeySequence)
+ sequence = m_shortcut.value<QKeySequence>();
else
sequence = QKeySequence::fromString(m_shortcut.toString());
QGuiApplicationPrivate::instance()->shortcutMap.removeShortcut(m_shortcutId, this, sequence);
diff --git a/tests/auto/quickcontrols/platform/data/tst_menuitem.qml b/tests/auto/quickcontrols/platform/data/tst_menuitem.qml
index a446bf8c4d..230cc5eb9f 100644
--- a/tests/auto/quickcontrols/platform/data/tst_menuitem.qml
+++ b/tests/auto/quickcontrols/platform/data/tst_menuitem.qml
@@ -4,6 +4,7 @@
import QtQuick
import QtTest
import Qt.labs.platform
+import org.qtproject.Test
TestCase {
id: testCase
@@ -63,6 +64,30 @@ TestCase {
compare(spy.count, 1)
}
+ function test_shortcut() {
+ if (!TestHelper.shortcutsSupported)
+ return;
+
+ let item = createTemporaryObject(menuItem, testCase)
+ verify(item)
+ let spy = createTemporaryObject(signalSpyComponent, testCase, {
+ target: item, signalName: "triggered"
+ })
+ verify(spy)
+ verify(spy.valid)
+
+ data = [TestHelper.shortcutInt, TestHelper.shortcutString, TestHelper.shortcutKeySequence]
+ for (let i = 0; i < data.length; ++i) {
+ item.shortcut = data[i]
+
+ compare(spy.count, i)
+ keySequence("CTRL+P")
+ compare(spy.count, i + 1)
+
+ item.shortcut = {}
+ }
+ }
+
function test_role() {
// Q_ENUMS(QPlatformMenuItem::MenuRole)
compare(MenuItem.NoRole, 0)
diff --git a/tests/auto/quickcontrols/platform/tst_platform.cpp b/tests/auto/quickcontrols/platform/tst_platform.cpp
index 94bf1d6a59..9cc3a2769c 100644
--- a/tests/auto/quickcontrols/platform/tst_platform.cpp
+++ b/tests/auto/quickcontrols/platform/tst_platform.cpp
@@ -4,11 +4,21 @@
#include <QtQml/qqmlengine.h>
#include <QtQuickTest/quicktest.h>
+using namespace Qt::Literals;
+
class Setup : public QObject
{
Q_OBJECT
Q_PROPERTY(bool shortcutsSupported READ areShortcutsSupported CONSTANT FINAL)
+ Q_PROPERTY(int shortcutInt MEMBER m_shortcutInt CONSTANT FINAL)
+ Q_PROPERTY(QString shortcutString MEMBER m_shortcutString CONSTANT FINAL)
+ Q_PROPERTY(QKeySequence shortcutKeySequence MEMBER m_shortcutKeySequence CONSTANT FINAL)
+
+ const int m_shortcutInt = QKeySequence::Print;
+ const QString m_shortcutString = u"CTRL+P"_s;
+ const QKeySequence m_shortcutKeySequence{ Qt::CTRL | Qt::Key_P };
+
public:
bool areShortcutsSupported() const
{