From fcbaa9344ddb7eae89759240f17e1f46df5c97d8 Mon Sep 17 00:00:00 2001 From: Paul Yushkevich Date: Tue, 1 Oct 2024 11:54:35 -0400 Subject: [PATCH] Working on a testing issue on Linux --- Testing/GUI/Qt/SNAPTestQt.cxx | 35 +++++++++++++++++++++-------------- Testing/GUI/Qt/SNAPTestQt.h | 4 ++++ 2 files changed, 25 insertions(+), 14 deletions(-) diff --git a/Testing/GUI/Qt/SNAPTestQt.cxx b/Testing/GUI/Qt/SNAPTestQt.cxx index 878d3fdb..97725286 100644 --- a/Testing/GUI/Qt/SNAPTestQt.cxx +++ b/Testing/GUI/Qt/SNAPTestQt.cxx @@ -255,6 +255,24 @@ void SNAPTestQt::application_exit(int rc) Q_ARG(int, rc)); } +void SNAPTestQt::postKeyEventInternal(QObject *object, QString key) +{ + QWidget *widget = dynamic_cast(object); + if(widget) + { + QKeySequence seq(key); + if(seq.count() == 1) + { + QKeyCombination code = seq[0]; + Qt::Key key = code.key(); + Qt::KeyboardModifiers mods = code.keyboardModifiers(); + + QKeyEvent *ev = new QKeyEvent(QEvent::KeyPress, key, mods); + QApplication::postEvent(widget, ev); + } + } +} + void SNAPTestQt::sleep(int milli_sec) { // Scale requested sleep time by acceleration factor @@ -332,20 +350,9 @@ void SNAPTestQt::postMouseEvent(QObject *object, double rel_x, double rel_y, QSt void SNAPTestQt::postKeyEvent(QObject *object, QString key) { - QWidget *widget = dynamic_cast(object); - if(widget) - { - QKeySequence seq(key); - if(seq.count() == 1) - { - QKeyCombination code = seq[0]; - Qt::Key key = code.key(); - Qt::KeyboardModifiers mods = code.keyboardModifiers(); - - QKeyEvent *ev = new QKeyEvent(QEvent::KeyPress, key, mods); - QApplication::postEvent(widget, ev); - } - } + // We need the code to run in the main thread + QMetaObject::invokeMethod( + this, "postKeyEventInternal", Qt::QueuedConnection, Q_ARG(QObject *, object), Q_ARG(QString, key)); } diff --git a/Testing/GUI/Qt/SNAPTestQt.h b/Testing/GUI/Qt/SNAPTestQt.h index d71e4042..36eb740d 100644 --- a/Testing/GUI/Qt/SNAPTestQt.h +++ b/Testing/GUI/Qt/SNAPTestQt.h @@ -116,6 +116,10 @@ public slots: static void application_exit(int rc); +protected slots: + + void postKeyEventInternal(QObject *object, QString key); + protected: ReturnCode ListTests();