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 8692607
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T00:20:50+00:00 2026-06-13T00:20:50+00:00

so i have a worker class that has 2 slots: StartWork() and StopWork(), the

  • 0

so i have a worker class that has 2 slots: StartWork() and StopWork(), the StartWork() one runs an infinite loop (it just reads and reads camera input non-stop) and the StopWork() method just sets a bool variable to false (so the loop inside StartWork() stops).

according to the QThread documentation, the best way to use them now is not by sub-classing but by moving workers into the thread, so that’s what i do. problem is, the started() signal from the thread gets called but the finished() signal never gets called.

worker class slots:

void StartWork(){ running = true; while(running){ do work; }}

void StopWork(){ running = false; }

QThread initialization and signal/slot connection:

thread = new QThread();
worker = new Worker();
worker.moveToThread(thread);

QObject::connect(thread, SIGNAL(started()), worker, SLOT(StartWork()));
QObject::connect(thread, SIGNAL(finished()), worker, SLOT(StopWork()));

and on my QPushButton i do this:

if(pushStartStop->text().toLower() == "start")
{
    pushStartStop->setText("Stop");
    thread->start();
}
else
{
    pushStartStop->setText("Start");
    thread->quit();
}

the thread->start() works fine, and the StartWork() gets called and everything is beautiful (GUI runs with no blocks, etc).

but thread->quit() doesn’t do anything, it gets called (because the button changes text) but thats it. if i just call worker->StopWork() it works, but then i can’t start it again.

I’ve tried with thread->exit(); but the results are the same. Also i know sub-classing works, but it looks uglier and according to the recent Qt documentation, sub-classing is no longer optimal.

thanks in advance.

  • 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-06-13T00:20:52+00:00Added an answer on June 13, 2026 at 12:20 am

    You have a forever loop:

    void StartWork()
    {
        running = true;
        while(running)
        {
            do work;
        }
    }
    

    This function will loop, so the event loop of the thread is blocked just after having emitted started(). Thus, finished() cannot be emitted.

    Solution 1:

    Add the function

    void DoWork()
    {
        if(running)
        {
            do work
        }
    }
    

    To Worker, and change

    void StartWork()
    {
        running = true;
    }
    

    Then, just connect DoWork to a timer in the thread.

    Solution 2:

    Change the function

    void StartWork()
    {
        running = true;
        while(running)
        {
            do work;
            QCoreApplication::processEvents();
        }
    }
    

    With this solution, you will have to restart the thread when the worker stops its job (StopWork() will force this function to finish, so the event loop will have no events to handle and the thread will be finished).

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

Sidebar

Related Questions

I have a layered worker class that I'm trying to get progress reports from.
I have a worker thread in a class that is owned by a ChildView.
lets say i have a background worker in a class that perform db query
I have a class that spawns an arbitrary number of worker object that compute
I have a worker class that does stuff with a collection of objects. I
I have a class (which I have no control over) that has a long
I have a class that has some long running methods, in which they do
I have here an myAction function in some controller. And it has one class
I have a static class that creates several worker threads in its constructor. If
I have a class that has an explicit initialization method and some other methods

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.