Details
-
Bug
-
Resolution: Cannot Reproduce
-
P3: Somewhat important
-
4.3.0
-
None
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)
void mousePressEvent(QMouseEvent *)
{ generateEvent(); }public slots:
void generateEvent()
void onShortcutInvoked()
{ qDebug() << "foo"; }};
#include "main.moc"
int main(int argc, char **argv)
{
QApplication a(argc, argv);
Widget w;
w.show();
return a.exec();
}