Important: Please read the Qt Code of Conduct -

Connecting quit signal in main.cpp for Qt.quit()

  • I have an app shell written by others:

    #include <QGuiApplication>
    #include <QQmlApplicationEngine>
    int main(int argc, char *argv[])
        QGuiApplication app(argc, argv);
        QQmlApplicationEngine engine;
        if (engine.rootObjects().isEmpty()) return -1;
        return app.exec();

    I want Qt.quit() to work in my application. Based on this forum answer from 2014 from @SGaist I added the following line before the return:

    QObject::connect(engine, &QQmlApplicationEngine::quit, &app, &QGuiApplication::quit);

    This fails to compile with the error:

    main.cpp:9: error: no matching function for call to ‘QObject::connect(QQmlApplicationEngine&, void (QQmlEngine::*)(), QGuiApplication*, void (*)())’

    The combination of the heavily-overloaded connect() method and my general lack of Qt/C++ knowledge leaves me confused. Under Qt 5.9 how should I modify my main so that I can call Qt.quit() from QML?

  • Moderators

    @Phrogz said in Connecting quit signal in main.cpp for Qt.quit():

    QObject::connect(engine, &QQmlApplicationEngine::quit, &app, &QGuiApplication::quit);

    I'm not fimilar with the issue, but just by looking at your connect statement, I see a typo:

    QObject::connect(engine, &QQmlApplicationEngine::quit, &app, &QGuiApplication::quit);
    // to ->
    QObject::connect(&engine, &QQmlApplicationEngine::quit, &app, &QGuiApplication::quit);

    Maybe that solves all issues ;-)

  • @J.Hilk That solved it. Thank you!

Log in to reply