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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T03:49:57+00:00 2026-06-09T03:49:57+00:00

I have a QThread which does a lot of calculations (it can run for

  • 0

I have a QThread which does a lot of calculations (it can run for minutes), and at one (and only one) point it requires user input, for example in the form of a yes/no dialog. Of course, no GUI elements can be accessed and no dialogs opened from the thread (a Qt design choice), as it is not the main thread.

Well there are many obvious solutions, but I’m interested in a “recommended” solution, or a “best practice”.

My ideas:

  1. as there is only one point where input must be read from the GUI, I can have two threads, the second thread being started after the dialog is evaluated. Problem: it makes the code inflexible, what if I have to introduce more dialogs later? Unlikely, but might happen.
  2. I have only one thread, and I communicate with signals and slots in both directions (I only have experience with signals in the form of “thread to main”, not in the reverse direction). So the thread runs, comes to the point where the user decision must be made, so the thread emits a signal to the main (aka the GUI thread), the main catches it in a slot, creates the dialog, evaluates the result, and emits a signal to the thread. What now? The thread catches the signal in a slot, but how should it affect the run() method where the calculations are being done? If the run() exits, the thread dies. So I have something like this in my run() function: while (!can_continue) { sleep(); } and I set can_continue in the slot where I caught the signal sent form the main. However, I have some doubts about this being the most simple / most elegant solution. Is there a general practice I should know about?
  • 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-09T03:49:58+00:00Added an answer on June 9, 2026 at 3:49 am

    Your problems with second version arises because you are working with Qt thread wrong.

    You should create new object class Worker: public QObject that has signals:

    void stage1Finished();
    void stage2Fibished();
    

    and slots:

    void startStage1();
    void startStage2();
    

    then create Qthread thread object, push Worker to the thread, connect startStage1() with started() signal of thread, show dialog on signal stage1Finished() and connect dialog-accepted-signal with startStage2(). Connect stage2Finished with exit() slot of thread.

    Then you will not have to sleep anywhere, all signals processing will go through standard mechanism in Qthread. Then you just start the thread to start processing and get finished signal on processing finished.

    While connecting signals to slots, use Qt::QueuedConnection.

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

Sidebar

Related Questions

Can a class inherited from QThread and having run method can have other methods
Have deployed numerous report parts which reference the same view however one of them
I have a C++/Qt QThread worker thread, which stores its current status as an
I have a worker QThread in which a QObject derived object is created. The
How can I wake up a QThread when it is sleeping? I have a
I have a server to which multiple clients can connect to. The client is
I have the main (GUI) thread which creates a QThread. In the QThread, I
I have two classes one and two . Both run threads. class Two is
I have a QThread which runs, but will often need to be killed, then
I have a multi-threaded application written in Python in which one thread takes care

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.