[Interest] Emitting signal from QThread::run()

Prav pr12og2 at programist.ru
Mon Feb 29 14:37:46 CET 2016


> Den 27-02-2016 kl. 07:15 skrev Syam:
>>
>> Hello,
>>
>> Inspired from the recent discussions on QThread, I have the following
>> question.
>> I have:
>>
>> class MyThread : public QThread
>> {
>>     Q_OBJECT
>>      //the constructor and other stuff
>>
>> signals:
>>       void mySignal();
>>
>> protected:
>>    virtual void run()
>>    {
>>       while(true)
>>       {
>>          //do something
>>          if(some_condition) emit mySignal();
>>       }
>>    }
>> };
>>
>>
>> void MainWindow::someFunction()
>> {
>>     MyThread *thread = new MyThread;
>>     connect(thread, SIGNAL(mySignal()), this, SLOT(myGuiSlot()),
>> Qt::QueuedConnection);
>>     thread->start();
>> }
>>
>>
>> //////////////////
>>
>> Will the above code work fine? In myGuiSlot() I am typically
>> manipulating some widgets - setting the text of a QLabel etc.

> Don't ever do this. I don't subscribe to the idea that you should never
> subclass QThread yourself, but I think it's a very bad idea to subclass
> and use signals and slots.

Why you so easily mixed "signals and slots" in this statement.
Signals are NOT slots ... so if slots in subclassed from QThread object usually gives not what people
naturally expect, it does not mean that signals are dangerous too.

Signals are just methods like any user's calculation code. What is wrong with them?
So without argumentation I do not get why signals in QThread::run() is better to avoid.




More information about the Interest mailing list