Details
-
Bug
-
Resolution: Unresolved
-
P2: Important
-
None
-
6.2.3
-
None
Description
QShortcut does not work with a QtQuick application using QApplication. The reason for this is because of qWidgetShortcutContextMatcher and QtWidgetsShortcutPrivate. Even though the shortcut is recognized the context does not match so the shortcut is never activated.
This means that you can't use QShortcut with the QML libraries that use QApplication like the QtChart package. This bug relates to QTBUG-76493, QTBUG-79638, and QTBUG-90924.
CMakeLists.txt:
cmake_minimum_required(VERSION 3.0) project(example LANGUAGES CXX) find_package(Qt6 COMPONENTS Widgets Quick Qml REQUIRED) add_executable(example main.cpp) target_link_libraries(example Qt::Widgets Qt::Quick Qt::Qml)
main.cpp:
#include <QtGui/QShortcut> #include <QtQuick/QQuickView> #include <QtWidgets/QApplication> int main(int argc, char* argv[]) { // QGuiApplication app(argc, argv); // Works QApplication app(argc, argv); // Does not work QQuickView view; auto shortcut = QShortcut(QKeySequence(QLatin1String("ctrl+b")), &view, []() { qDebug() << "Hello"; }); view.show(); return app.exec(); }