Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. How to prevent QML events to happen after QCoreApplication::aboutToQuit or make sure a C++ function will be last?
Forum Updated to NodeBB v4.3 + New Features

How to prevent QML events to happen after QCoreApplication::aboutToQuit or make sure a C++ function will be last?

Scheduled Pinned Locked Moved Solved QML and Qt Quick
3 Posts 2 Posters 432 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • mbruelM Offline
    mbruelM Offline
    mbruel
    wrote on last edited by
    #1

    Hi,
    I'm having a main cpp instance Qrop which is a QObject and a singleton.
    in the main.cpp, I'm connecting QCoreApplication::aboutToQuit to its destruction (cf here)
    My main qrop cpp object and some of its members are used in QML making sure their ownership (deletion) is made in C++.

    The main methods looks like this:

    int main(int argc, char *argv[])
    ...
        Qrop *qrop = Qrop::instance();
        QObject::connect(&app, &QCoreApplication::aboutToQuit, &Qrop::clear);
        int res = qrop->init();
        if (res != 0)
            return res;
    
        FamilyService *svcFamily = qrop->familyService();
        QQmlApplicationEngine engine;
        engine.rootContext()->setContextProperty("cppQrop", qrop);
        engine.rootContext()->setContextProperty("cppFamily", svcFamily);
    
        // really important, otherwise QML could take ownership and delete them
        // (cf http://doc.qt.io/qt-5/qtqml-cppintegration-data.html#data-ownership )
        engine.setObjectOwnership(qrop->buildInfo(), QQmlEngine::CppOwnership);
        engine.setObjectOwnership(qrop->news(), QQmlEngine::CppOwnership);
        engine.setObjectOwnership(svcFamily->modelFamily(), QQmlEngine::CppOwnership);
        engine.setObjectOwnership(svcFamily->modelSeedCompany(), QQmlEngine::CppOwnership);
    
        //    QQmlFileSelector *selector = new QQmlFileSelector(&engine);
        const QUrl url(QStringLiteral("qrc:/qml/MainWindow.qml"));
        QObject::connect(&engine, &QQmlApplicationEngine::objectCreated, &app,
                         [url](QObject *obj, const QUrl &objUrl) {
                             if (!obj && url == objUrl)
                                 QCoreApplication::exit(-1);
                         },
                         Qt::QueuedConnection);
        engine.load(url);
        engine.addImageProvider("pictures", new QrpImageProvider());
    
        if (qrop->hasErrors())
            qrop->showErrors();
    
        qrop->news()->fetchNews();
    
        return QApplication::exec();
    }
    

    I've noticed the App is crashing when I close the Window due to a currentIndexChanged event on a Combobox happening after my C++ objects are destructed (so after the QCoreApplication::aboutToQuit). The crash is simple, in the handler I'm using one of my deleted C++ objects... cf here

    I've made a hack that is working to prevent the crash by emitting an aboutToQuit signal from my main Qrop object in its destructor (cf here) which is connected in QML Combobx (cf here) but I don't find this really pretty...

    Is there a way to make sure a C++ handler would be called last? meaning no more QML events?
    What would be the regular way to do that?

    1 Reply Last reply
    0
    • sierdzioS Offline
      sierdzioS Offline
      sierdzio
      Moderators
      wrote on last edited by
      #2

      Instead of using Qrop as singleton in main.cpp - put it on stack, then it will get deleted when main gets out of scope. Or make QQmlEngine a parent of Qrop, then parent-child relationships will take care of the cleanup.

      You can continue using Qrop as singleton in other parts of the app, this is only about main.

      (Z(:^

      mbruelM 1 Reply Last reply
      2
      • sierdzioS sierdzio

        Instead of using Qrop as singleton in main.cpp - put it on stack, then it will get deleted when main gets out of scope. Or make QQmlEngine a parent of Qrop, then parent-child relationships will take care of the cleanup.

        You can continue using Qrop as singleton in other parts of the app, this is only about main.

        mbruelM Offline
        mbruelM Offline
        mbruel
        wrote on last edited by
        #3

        @sierdzio hum indeed easy... Thanks!
        I've made the QML engine parent of my main C++ singleton and its working like expected \o/ (cf changes (ignore new cmd.cpp file))

        my first implementation of the singleton was using a static object on the stack and if I remember well it wasn't deleted. That is why I went for a heap solution and used that QCoreApplication::aboutToQuit connection to destroy it.

        1 Reply Last reply
        0

        • Login

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved