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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T22:08:05+00:00 2026-06-17T22:08:05+00:00

I work in Qt and when I press the button GO I need to

  • 0

I work in Qt and when I press the button GO I need to continuously send packages to the network and modify the interface with the information I receive.

The problem is that I have a while(1) in the button so the button never finishes so the interface is never updated. I thought to create a thread in the button and put the while(){} code there.

My question is how can I modify the interface from the thread? (For example how can I modify a textBox from the thread ?

  • 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-17T22:08:06+00:00Added an answer on June 17, 2026 at 10:08 pm

    Important thing about Qt is that you must work with Qt GUI only from GUI thread, that is main thread.

    That’s why the proper way to do this is to notify main thread from worker, and the code in main thread will actually update text box, progress bar or something else.

    The best way to do this, I think, is use QThread instead of posix thread, and use Qt signals for communicating between threads. This will be your worker, a replacer of thread_func:

    class WorkerThread : public QThread {
        void run() {
            while(1) {
                 // ... hard work
                 // Now want to notify main thread:
                 emit progressChanged("Some info");
            }
        }
        // Define signal:
        signals:
        void progressChanged(QString info);
    };
    

    In your widget, define a slot with same prototype as signal in .h:

    class MyWidget : public QWidget {
        // Your gui code
    
        // Define slot:
        public slots:
        void onProgressChanged(QString info);
    };
    

    In .cpp implement this function:

    void MyWidget::onProgressChanged(QString info) {
        // Processing code
        textBox->setText("Latest info: " + info);
    }
    

    Now in that place where you want to spawn a thread (on button click):

    void MyWidget::startWorkInAThread() {
        // Create an instance of your woker
        WorkerThread *workerThread = new WorkerThread;
        // Connect our signal and slot
        connect(workerThread, SIGNAL(progressChanged(QString)),
                              SLOT(onProgressChanged(QString)));
        // Setup callback for cleanup when it finishes
        connect(workerThread, SIGNAL(finished()),
                workerThread, SLOT(deleteLater()));
        // Run, Forest, run!
        workerThread->start(); // This invokes WorkerThread::run in a new thread
    }
    

    After you connect signal and slot, emiting slot with emit progressChanged(...) in worker thread will send message to main thread and main thread will call the slot that is connected to that signal, onProgressChanged here.

    P.s. I haven’t tested the code yet so feel free to suggest an edit if I’m wrong somewhere

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

Sidebar

Related Questions

I'm looking to work on an iOS app that would need to send/receive data
Need to make a reaction. When you press the button on the ItemsControl that
So I have a program that after a try/catch block occurs I need a
I have a table with static cells. I also have a button that sets
I have the problem that in Notification Center widgets touch events are not being
I use global variable $table_prefix to differ whether I work on Word Press or
In my apps i find the need to have infinite while loops mostly to
I have a routine that draws a gearwheel using CoreGraphics and drawRect. I used
I have a html file with few pages, and when a user press a
Creating JQGRID object i put hiddengrid:true. Now, to expand grid i need to press

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.