Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. General talk
  3. Qt 6
  4. How to properly delete QThread?
QtWS25 Last Chance

How to properly delete QThread?

Scheduled Pinned Locked Moved Solved Qt 6
2 Posts 2 Posters 503 Views
  • 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.
  • C Offline
    C Offline
    Christina123
    wrote on last edited by
    #1

    Hi, I have a QThread in my main dialog which is responsible for some processes declared in MyWorker class (inherited from QObject). I wish that when I close the dialog it will automatically delete the thread and MyWorker object. However, each time I close the window I get the message QThread: Destroyed while thread is still running 14:14:16: The program has unexpectedly finished.
    What I try to do is in my dialog's deconstructor, I emit a signal void finished() which is connected to the thread and the object. Nevertheless, it does work. So, what will be the correct way to end the thread and its process?

    connect(this, &Sender::finished, sendThread, &MyThread::deleteLater);
    connect(this, &Sender::finished, &sThread, &QThread::deleteLater);
    connect(this, &Sender::finished, &sThread, &QThread::quit);
    
    1 Reply Last reply
    0
    • Christian EhrlicherC Online
      Christian EhrlicherC Online
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on last edited by Christian Ehrlicher
      #2

      You have to wait() until the thread had time to shut down. Also you should not connect Sender::finished to deleteLater but the QThread's finished signal to deletedLater.

      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
      3

      • Login

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