[Qt-interest] QThread Problem

Bradley T. Hughes bradley.hughes at nokia.com
Fri Feb 12 10:26:23 CET 2010


On 02/11/2010 03:13 PM, Yuvraj wrote:
> Hi
>
> I want to run separate from main thread;
>
> My code is
>
> *my_thread.cpp*
>
> #include "my_thread.h"
> #include<QDebug>
> #include<QString>
>
> my_thread::my_thread()
> {
> this->start();

I would advise against call start() from your constructor.

> moveToThread(this);

You really don't want to do this unless you *REALLY* understand what it 
does. If you don't, and just found some suggestion from someone else that 
also did it, don't do it.

> }
> void my_thread::run()
> {
> this->exec();

exec() is a blocking call... it will not return until you explicitly tell 
the thread to quit().

> this->display();
>
> }
> void my_thread::display()
> {
> while(1)
> {
> qDebug()<<  "Sub thread is running";
> }
> }
> *my_thread.h
>
>
> #ifndef MY_THREAD_H
> #define MY_THREAD_H
> #include<QThread>
> #include "mainwindow.h"
> #include<QObject>
> class my_thread:public QThread
> {
> public:
> my_thread();
> void run();
> void display();
>
>
> };
>
>
> #endif // MY_THREAD_H
>
>
> i am calling the thread as follows
>
> my_thread thread;
> thread.run();

You should not call run() yourself, QThread does this for you after you call 
start().

> while(1)
> {
> qDebug()<<  "Main thread is running !!!\n";
> }
>
> is it correct ? If any thing wrong please let me know
>
>
> Thanks
>
> Yuvraj
>
> *


-- 
Bradley T. Hughes (Nokia-D-Qt/Oslo), bradley.hughes at nokia.com
Sandakervn. 116, P.O. Box 4332 Nydalen, 0402 Oslo, Norway



More information about the Qt-interest-old mailing list