Q:

Qt Auto-UI Testing stopping because of messagebox. How to simulate enter on messagebox?

My task is to write an automated UI test for a software being developed. It happens so, that there are radiobuttons triggering a messagebox, which stops my automated test until I manually confirm it (pressing ENTER). The issue is that I do not know how I can call that newly summoned messagebox and then have it confirmed by QTest::keyClick(<object>, QtKey::Key_Enter); and have my automated test continue the run.
I am using QWidgets, QApplication, Q_OBJECT and QtTest.
I will provide a code similar to what I am working with:
void testui1(){
Form1* myform = new Form1();
myform->show();
QTest::mouseClick(myform->radioButton01, Qt::LeftButton, Qt::NoModifier, QPoint(), 100);
// Message box pops up and stops the operation until confirmed
QTest::mouseClick(myform->radioButton02, Qt::LeftButton, Qt::NoModifier, QPoint(), 100);
// again
...
}

How exactly can I script to confirm the message box automatically? The message box is only an [OK] type, so I don't need it to return whether I have pressed Yes or No. A QTest::keyClick(<target object>, Qt::Key_Enter) method needs to know to which object it should press enter to. I tried including myform into the object and it did not work. Googling I did not find the answer. I found the following result as not functioning for what I am looking for
QWidgetList allToplevelWidgets = QApplication::topLevelWidgets();
foreach (QWidget *w, allToplevelWidgets) {
    if (w->inherits("QMessageBox")) {
        QMessageBox *mb = qobject_cast<QMessageBox *>(w);
        QTest::keyClick(mb, Qt::Key_Enter);
    }
}

A:

The problem is that once you've "clicked" on your radio button, which results in QMessageBox::exec being called, your code stops running until the user clicks on of the buttons.
You can simulate the user clicking a button by starting a timer before you click on the radio button. 
In the callback function for the timer you can use QApplication::activeModalWidget() to obtain a pointer to the message box, and then just call close on it.
QTimer::singleShot(0, [=]()
    {
        QWidget* widget = QApplication::activeModalWidget();
        if (widget)
            widget->close();
    });

If you want to press a specific key on the message box, you can use QCoreApplication::postEvent to post a key event to the message box
QTimer::singleShot(0, [=]()
    {
        int key = Qt::Key_Enter; // or whatever key you want

        QWidget* widget = QApplication::activeModalWidget();
        if (widget)
        {
            QKeyEvent* event = new QKeyEvent(QEvent::KeyPress, key, Qt::NoModifier); 
            QCoreApplication::postEvent(widget, event);
        }
    });

So in terms of how this ties into the sample code you gave:
void testui1()
{
    Form1* myform = new Form1();
    myform->show();

    QTimer::singleShot(0, [=]()
        {
            QWidget* widget = QApplication::activeModalWidget();
            if (widget)
                widget->close();
            else
                QFAIL("no modal widget");
        });

    QTest::mouseClick(myform->radioButton01, Qt::LeftButton, Qt::NoModifier, QPoint(), 100);
}

A sample app putting all the above together:
#include <QApplication>
#include <QMainWindow>
#include <QHBoxLayout>
#include <QPushButton>
#include <QTimer>
#include <QMessageBox>
#include <iostream>

int main(int argc, char** argv)
{
    QApplication app(argc, argv);
    QMainWindow window;

    QWidget widget;
    window.setCentralWidget(&widget);

    QHBoxLayout layout(&widget);

    QPushButton btn("go");
    layout.addWidget(&btn);

    QObject::connect(&btn, &QPushButton::clicked, [&]()
        {
            QTimer::singleShot(0, [=]()
                {
                    QWidget* widget = QApplication::activeModalWidget();
                    if (widget)
                    {
                        widget->close();
                    }
                    else
                    {
                        std::cout << "no active modal\n";
                    }
                });

            QMessageBox(
                QMessageBox::Icon::Warning,
                "Are you sure?",
                "Are you sure?",
                QMessageBox::Yes | QMessageBox::No,
                &window).exec();
        });

    window.show();
    return app.exec();
}

Clicking on Go will look like nothing is happening, as the QMessageBox is closed immediately. 
To prove that the message box is shown and then closed you can increase the time in the call to QTimer::singleShot to a value such as 1000. Now it will show the message box for 1 second, and then it will be closed by the timer.

