Trolltech Home | Qt-interest Home | Recent Threads | All Threads | Author | Date
All threads index page 3

Qt-interest Archive, April 2008
Moving a QThread object to its own thread ?


Message 1 in thread

Hello,

As stated by the doc, a QThread object belongs to thread that created
it. Therefore, if a QThread-derived class declares slots, they will
not be executed in itself, but in the parent thread. A common way
around the problem is to create a proxy object in the run function of
the thread, and put the slots in there.

I was wondering if it was possible to avoid the proxy object by simply
moving the QThread object to itself :

class MyThread : public QThread
{
public:
    MyThread()
    {
        moveToThread(this);
    }
};

In my very limited testing, this seems to work, but I was wondering if
I could rely on this, or if there was some inherent problem with the
approach ? For example, the doc doesn't state whether you can move an
object to a thread that hasn't started yet ...

-- 
 [ signature omitted ] 

Message 2 in thread

ewww. dont do that! 
since 4.4 (or 4.3 ? )   QThread is non pure. you can just construct one and 
push whatever you need on it.

QThread mythread;
mythread.start();
MyQObject myobject;
myobject.moveToThread(&mythread);

there are also various helper librarys around that make threads a lot nicer.

On Friday 11 April 2008 19:07:08 Julien Cugnière wrote:
> I was wondering if it was possible to avoid the proxy object by simply
> moving the QThread object to itself :
>
> class MyThread : public QThread
> {
> public:
>     MyThread()
>     {
>         moveToThread(this);
>     }
> };


-- 
 [ signature omitted ]