0

In my (Qt-)program I need a continuous request of a value which I get from an external source. But I did not want that this request freezes the whole program, so I created a separate thread for this function. But even if it is running in a separate thread, the GUI freezes, too. Why?

Code for the request function:

void DPC::run()
{
    int counts = 0, old_counts = 0;
    while(1)
    {
        usleep(50000);
        counts = Read_DPC();
        if(counts != old_counts)
        {
            emit currentCount(counts);
            old_counts = counts;
        }

    }
}

Read_DPC() returns an int value I want to sent to a lineEdit in my GUI.
The main class looks like

class DPC: public QThread
{
    Q_OBJECT
public:
    void run();
signals:
    void currentCount(int);
};

This code is called in the main function as:

DPC *newDPC = new DPC;
connect(newDPC, SIGNAL(currentCount(int)), SLOT(oncurrentCount(int)));
connect(newDPC, SIGNAL(finished()), newDPC, SLOT(deleteLater()));
newDPC->run();

How can I prevent this code from freezing my GUI? What am I doing wrong? Thanks!

7
  • How exactly are you launching a separate thread? Commented Oct 15, 2014 at 17:39
  • I thought via creating a subclass which is derived from QThread? According to some threads (stackoverflow.com/questions/14545961/…) or (stackoverflow.com/questions/16501284/…) Commented Oct 15, 2014 at 17:40
  • 2
    Why do you call run from the main thread? Didn't you want some other thread to call run? Wasn't that the whole point of deriving from QThread? Commented Oct 15, 2014 at 17:51
  • How should I start the subthread if not using run? I tried using start, but that did not work, too (I simply replaced run with start, if that was correct) Commented Oct 15, 2014 at 17:52
  • @arc_lupus One bug at a time. Fix the call to run and then re-test, describing in as much detail as possible what goes wrong after that bug is fixed. Commented Oct 15, 2014 at 18:57

1 Answer 1

5

It seems that you code run in GUI thread because you use run() method to start thread, so try to call start() as documentation and many examples said.

Try:

DPC *newDPC = new DPC;
connect(newDPC, SIGNAL(currentCount(int)), SLOT(oncurrentCount(int)));
connect(newDPC, SIGNAL(finished()), newDPC, SLOT(deleteLater()));
newDPC->start();//not run

Anyways you can call thread() method or currentThread() to see in which thread some objects live.

Sign up to request clarification or add additional context in comments.

17 Comments

Using start() leads to freeze, too.
@arc_lupus add qDebug() << thread() to your mainWindow and in the run() Did you get same threads? What exactly you get? Same threads means same adress in the output
@arc_lupus I run same code right now and try to undestand problem. Tell me please what you can see when in run you'll wrote(in while loop): qDebug() << thread() << currentThread();
@arc_lupus when I use run instead of start my GUI freeze too, call run is wrong, now I try understand why start() do not work for you.
@N1ghtLight I will, it was cool.There is no any result from voting up comment, but your contribution will not be forgotten or omitted ;)
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.