Uploaded image for project: 'Qt'
  1. Qt
  2. QTBUG-100178

QShortcut does not work with QtQuick apps using QApplication

    XMLWordPrintable

Details

    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();
      }
      

      Attachments

        No reviews matched the request. Check your Options in the drop-down menu of this sections header.

        Activity

          People

            srutledg Shawn Rutledge
            jeremykim232323 Jeremy Kim
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

            Dates

              Created:
              Updated:

              Gerrit Reviews

                There are no open Gerrit changes