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?
Forum Updated to NodeBB v4.3 + New Features

How to properly delete QThread?

Scheduled Pinned Locked Moved Solved Qt 6
2 Posts 2 Posters 528 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 11 Apr 2021, 13:18 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
    • C Offline
      C Offline
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on 11 Apr 2021, 14:29 last edited by Christian Ehrlicher 4 Nov 2021, 14:30
      #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

      2/2

      11 Apr 2021, 14:29

      • Login

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