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

problems with triggering shortcuts by sending keyEvents

    XMLWordPrintable

Details

    Description

      It seems to be very hard to test shortcuts with QTestLib.
      This example reproduces that triggering a QShortcut by generating an event works when done from within a mousePressEvent but otherwise not.

      (Whether it just creates a QKeyEvent like this and calls notify or uses the QTest convenience functions makes no difference. The setSpontaneous() doesn't help either)

      #include <QtTest/qtestspontaneevent.h>
      #include <QTimer>
      #include <QWidget>
      #include <QShortcut>
      #include <QKeySequence>
      #include <QMouseEvent>
      #include <QKeyEvent>
      #include <QEvent>
      #include <QApplication>
      #include <QDebug>

      class Widget : public QWidget
      {
      Q_OBJECT
      public:
      Widget(QWidget *parent = 0)
      : QWidget(parent)

      { new QShortcut(QKeySequence(Qt::Key_R), this, SLOT(onShortcutInvoked())); QTimer::singleShot(1000, this, SLOT(generateEvent())); }

      void mousePressEvent(QMouseEvent *)

      { generateEvent(); }

      public slots:
      void generateEvent()

      { QKeyEvent ev(QEvent::KeyPress, Qt::Key_R, Qt::NoModifier, "r"); reinterpret_cast<QSpontaneKeyEvent *>(&ev)->setSpontaneous(); qApp->notify(this, &ev); }

      void onShortcutInvoked()

      { qDebug() << "foo"; }

      };

      #include "main.moc"

      int main(int argc, char **argv)
      {
      QApplication a(argc, argv);
      Widget w;
      w.show();
      return a.exec();
      }

      Attachments

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

        Activity

          People

            jasmcdon Jason McDonald (Closed Nokia Identity. Please assign to "macadder" instead) (Inactive)
            rve Anders Bakken
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved:

              Gerrit Reviews

                There are no open Gerrit changes