Search code examples
qtcatch2

How do I integrate Catch2 with the QT Event Loop?


Some parts of QT rely on the event loop being up and running (or at least generate warnings otherwise). How do you integrate Catch2 tests with the QT event loop?


Solution

  • To integrate, adding a one shot timer will cause a function to run as soon as the app event loop is active. Here is what my code looks like:

    #define CATCH_CONFIG_RUNNER
    #include "catch.hpp"
    #include <QCoreApplication>
    #include <QTimer>
    int main(int argc, char *argv[]) {
      QCoreApplication app(argc, argv);
      QTimer::singleShot(0, [&]{
        app.exit(Catch::Session().run(argc, argv));
      });
      return app.exec();
    }
    

    Specifically for me, I was getting a bunch of warnings that said, "QSocketNotifier can only be used with threads started with QThread", but it turned out that really just meant it wanted an event loop to exist. This code here removed the warning for me.