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.