Pergunta

I need to automate gui tests in c++ qt using qtest (in eclipse) I have given a function that adds some tabs to a tabwidget (up to max 9) and if you try to open a 10th tab, a QMessageBox appears:

QMessageBox::critical(this, "MAX9",
tr("Only a maximum of 9 tabs can be opened.\n"));

Because the whole menu with the "add tab" function and everything is private, I had to access the method using slots and signals from my testclass.

Now my question is, is there a way I can check whether there are ANY QMessageBoxes open and if yes, automatically close them?

EDIT: SOLVED I put vahancho's solution into a method (CloseMessageBoxes) and I've created a timer in my testmethod that calls CloseMessageBoxes() method then:

QTimer *timer = new QTimer(this);
connect(timer, SIGNAL(timeout()), this, SLOT(CloseMessageBoxes()));
timer->start(5000);

There are multiple messageboxes appearing but all are closing themselves after 5 seconds.

Foi útil?

Solução

I think, you can find all message boxes as they are top level widgets, and close them one by one:

QWidgetList topWidgets = QApplication::topLevelWidgets();
foreach (QWidget *w, topWidgets) {
    if (QMessageBox *mb = qobject_cast<QMessageBox *>(w)) {            
        QTest::keyClick(mb, Qt::Key_Enter);
    }
}

However the problem is that message box is a modal dialog and it blocks the main event loop. You need to find a way to execute the code above after a message box appeared.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top