Test modal dialog with Qt Test

前端 未结 4 872
青春惊慌失措
青春惊慌失措 2021-01-05 09:34

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

4条回答
  •  醉酒成梦
    2021-01-05 10:34

    The solution I use in command line applications which use Qt libraries meant for GUIs is the singleShot, as this answer alludes. In those cases it looks like this:

    QCoreApplication app(argc, argv);
    
    // ...
    
    QTimer::singleShot(0, &app, SLOT(quit()));
    return app.exec();
    

    So in your case I imagine it would look something like this:

    QDialog * p_modalDialog = getThePointer(); // you will have to replace this with
                                               // a real way of getting the pointer
    
    QTimer::singleShot(0, p_modalDialog, SLOT(accept()));
    
    p_modalDialog->exec(); // called somewhere else in your case
                           // but it will be automatically accepted.
    

提交回复
热议问题