View Full Version : Testing modal dialogs with QTestLib

1st June 2010, 14:39
I haven't seen this discussed elsewhere, so I thought others might find it useful to know this.

I have an app (basically just one top level Widget) that puts up a modal QMessageBox at one point. I am the testing my Widget with QTestLib (env: Qt 4.6.2 and Windows). My problem was how to get aQTest::KeyClick() delivered to the modal dialog from a test function.

The thing is that when launching a modal dialog using either the static QMessageBox::information() function, or by calling exec() on a QMessageBox object, the UI (thread) is blocked and the test function can't call KeyClick() before the user dismisses the modal dialog manually - which means that user interaction is needed and the test cannot be run fully automatically. This is not what I wanted.

However, I found out that if my widget under testing launches the modal dialog with QMessageBox::open(), the test function will continue running and it can dismiss the dialog by calling QTest::KeyClick() on it (I am using qApp->activeModalWidget() to get a pointer to the dialog). So, the test is now fully automatic!

This works for me. But I am interested to hear if there are other ways to deal with this kind of problem?

31st May 2013, 16:34
Hi, I dont know if Your problem is exists yet or not, I found this solution:

void MyTest::testPasswordDialog()
QTimer::singleShot(500, this, SLOT(TimeOut()));

void MyTest::TimeOut()
QWidgetList allToplevelWidgets = QApplication::topLevelWidgets();
foreach (QWidget *w, allToplevelWidgets) {
if (w->inherits("QMessageBox")) {
QMessageBox *mb = qobject_cast<QMessageBox *>(w);
QTest::keyClick(mb, Qt::Key_Enter);