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. Final task upon program termination

Final task upon program termination

Scheduled Pinned Locked Moved Solved QML and Qt Quick
qt5qmlc++signal & slotfunction
3 Posts 2 Posters 825 Views 2 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.
  • T Offline
    T Offline
    texasRanger
    wrote on last edited by
    #1

    Hello,

    I was wondering if there is a way to run a final function call/ slot when a program terminates. I have a json file with data that is being updated and read from. When the program ends (user stops running program) I want to reset the data withing the file. Is there any type of signal slot that you be used right before before terminates?

    1 Reply Last reply
    0
    • fcarneyF Offline
      fcarneyF Offline
      fcarney
      wrote on last edited by
      #2

      Does you main.cpp look something like this?

      #include <QGuiApplication>
      #include <QQmlApplicationEngine>
      
      int main(int argc, char *argv[])
      {
          QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
      
          QGuiApplication app(argc, argv);
      
          QQmlApplicationEngine engine;
          const QUrl url(QStringLiteral("qrc:/main.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);
      
          return app.exec();
      }
      

      Then change the return statement to something like this:

      auto ret = app.exec()
      
      // your final code here
      finalfunction();
      
      return ret;
      
      

      C++ is a perfectly valid school of magic.

      T 1 Reply Last reply
      2
      • fcarneyF fcarney

        Does you main.cpp look something like this?

        #include <QGuiApplication>
        #include <QQmlApplicationEngine>
        
        int main(int argc, char *argv[])
        {
            QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
        
            QGuiApplication app(argc, argv);
        
            QQmlApplicationEngine engine;
            const QUrl url(QStringLiteral("qrc:/main.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);
        
            return app.exec();
        }
        

        Then change the return statement to something like this:

        auto ret = app.exec()
        
        // your final code here
        finalfunction();
        
        return ret;
        
        
        T Offline
        T Offline
        texasRanger
        wrote on last edited by
        #3

        @fcarney Sweet! That worked, thanks.

        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