I am trying to write a unit test for a GUI application using the QTestLib. The problem is that one of the slots creates a modal dialog using exec()
and I found no possibility to interact with the dialog.
The slots which creates the dialog is connected to a QAction. So the first problem is that the test blocks when I trigger the QAction in the test since this results in the call to exec()
. Therefore, I tried creating a QThread that performs the interaction. However, this did not help.
Things I already tried (all performed from within the "interaction helper" thread):
- Send key clicks using
QTest::keyClicks()
- Results in error message "QCoreApplication::sendEvent(): Cannot send events to objects owned by a different thread"
- Post QKeyEvents using
QCoreApplication::postEvent()
Doesn't work, i.e. nothing happens. I guess because the events end up in the event loop of the thread that owns the dialog, which will not be reached until the dialog is closed andSee Edit belowexec()
returns.
- Invoking Slots on the dialog using
QMetaObject::invokeMethod()
Doesn't work, i.e. nothing happens. I guess for the same reason asSee Edit belowpostEvent()
doesn't work.
So the question is: Is there any way to interact programmatically with a modal dialog that was opened using the exec()
method?
Edit: Actually, method 3 is working. The problem was a different one:
I passed the arguments to invokeMethod()
to the "interaction helper" thread and for some reason, accessing the arguments did not work from that thread (I got no SEG errors but they were simply empty).
I guess that method 2 is also working and I simply had the same problem as with method 3 but I didn't test that.