Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6581359
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T16:08:33+00:00 2026-05-25T16:08:33+00:00

I am trying to perform some work in a separate thread and stop that

  • 0

I am trying to perform some work in a separate thread and stop that thread when the work is done.

I have set up a connection like this

thread.connect( workerClass, SIGNAL( finished() ), SLOT( quit() ) );

but the quit() slot is never called when I emit the finished() signal. The command line does not show any warnings related to failed connections.

I created a simple test project to narrow down the problem and I get the same behavior:

TestClass.h:

#ifndef TESTCLASS_H
#define TESTCLASS_H
class TestClass : public QObject
{
    Q_OBJECT
public:
    TestClass() { }
signals:
    void finished();
public slots:
    void doWork();
}
#endif // TESTCLASS_H

TestClass.cpp:

#include "TestClass.h"
#include <QtCore/QDebug>
void TestClass::doWork()
{
    qDebug() << "Starting";
    for( long i = 0; i < 1000000000; i++ );
    qDebug() << "Done";
    emit finished();
};

and the main.cpp:

#include <QtCore/QCoreApplication>
#include <QtCore/QThread>
#include "TestClass.h"

int main( int argc, char* argv[] )
{
    QCoreApplication a( argc, argv );

    TestClass testClass;
    QThread testThread;

    testClass.moveToThread( &testThread );

    testThread.connect( &testClass, SIGNAL( finished() ), SLOT( quit() ) );
    testClass.connect( &testThread, SIGNAL( started() ), SLOT( doWork() ) );

    testThread.start();
    testThread.wait();

    return 0;
}

So here is what I expected to happen:

  1. Once testThread.start() gets processed, the thread’s run() function gets called, which internally calls exec() and starts an event loop. Additionally it throws a started() signal.
  2. testClasshas its doWork() slot called.
  3. When TestClass::doWork() is done, it throws a finished() signal.
  4. The event loop of testThread receives the signal and forwards it to its quit() slot
  5. testThread.wait() returns from waiting as the thread has ended.

Steps 1-3 work and I get the qDebug() output.
The problem is at step 4: The signal is fired and forwarded to some event loop (I debugged it a bit, but couldn’t figure out to which event loop it was fired) but the quit() slot is never called.

What am I doing wrong?

P.S.: Please don’t suggest subclassing QThread instead, the whole point of me trying this way out is because I want to avoid subclassing QThread.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T16:08:34+00:00Added an answer on May 25, 2026 at 4:08 pm

    I believe the issue is that you actually don’t have Qt event loop running in the scenario. If you replace testThread.wait() with a.exec() (which starts the Qt event loop) you should see your signals/slots getting processed properly.

    If you want the application to quit when your thread has finished you can use QCoreApplication’s quit() slot to do so.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to perform a simple operation that i have done alot in
I have a Rails 3 app that is trying to perform some controller action
I'm having some problems trying to perform a query. I have two tables, one
I am doing some automation work that requires several reboots. I am trying to
I am trying to perform some unit testing on the iphone but for some
I'm trying to perform some calculations on a non-directed, cyclic, weighted graph, and I'm
I am trying to perform some composition-based filtering on a large collection of strings
I am trying to perform some calculations with a form but every time i
I am using VS 2010 RTM and trying to perform some basic validation on
Trying to perform a single boolean NOT operation, it appears that under MS SQL

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.