Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Stop QCoreApplication running in C++11 thread
Forum Updated to NodeBB v4.3 + New Features

Stop QCoreApplication running in C++11 thread

Scheduled Pinned Locked Moved Solved General and Desktop
5 Posts 3 Posters 320 Views 3 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.
  • SebastianMS Offline
    SebastianMS Offline
    SebastianM
    wrote on last edited by
    #1

    Hi,
    I've got QCoreApplication running in C++11 thread. It created there and it call exec() to stay in event loop. This thread is created to process dbus communicaiton. Rest of application is unaware of Qt.
    Now I would like to kill this thread safely when whole application is closed.
    How to do it? Any useful techniques? Callback?

    1 Reply Last reply
    0
    • Christian EhrlicherC Offline
      Christian EhrlicherC Offline
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on last edited by
      #2

      See QCoreApplication::quit()

      Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
      Visit the Qt Academy at https://academy.qt.io/catalog

      1 Reply Last reply
      2
      • SebastianMS Offline
        SebastianMS Offline
        SebastianM
        wrote on last edited by
        #3

        Yes, I know.
        Below works as class Communicate exist as long as main class. I know it not nice - calling from one thread to other.

        class Communicate {
        ....
        void run();
        void * qt_app_ptr;
        }
        
        void run() {
        thread_holder = std::thread([this, qt_app_ptr]{
        	QCoreApplication app(argc, nullptr);
        	qt_app_ptr = &app;    
        	(.....)
        	app.exec()
        });
        }
        
        Communicate::~Communicate()
        {
          static_cast<QCoreApplication*>(qt_app_ptr)->quit();
          thread_holder.join();
        }
        
        1 Reply Last reply
        0
        • Christian EhrlicherC Offline
          Christian EhrlicherC Offline
          Christian Ehrlicher
          Lifetime Qt Champion
          wrote on last edited by
          #4

          No need to store a pointer to the app. Either use the static function QCoreApplication::exit(0) or qApp->quit().

          Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
          Visit the Qt Academy at https://academy.qt.io/catalog

          1 Reply Last reply
          1
          • Kent-DorfmanK Offline
            Kent-DorfmanK Offline
            Kent-Dorfman
            wrote on last edited by
            #5

            in generic sense: send signal to thread, trap signal, execute QApplication::quit

            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