Is it possible to move an object instance to different threads at different points of code in a QT project?
<p>I am trying to determine if you can move one object instance to different threads at different points during run-time. </p>

<p>Below is some sample code to show you what I mean:</p>

<pre><code>this-&gt;thread1           = new QThread( this );
this-&gt;thread2           = new QThread( this );

this-&gt;pObject-&gt;moveToThread( this-&gt;thread1 );

connect(this-&gt;thread1, SIGNAL(started()), this-&gt;pObject, SLOT(fnc1()));
connect(this-&gt;thread2, SIGNAL(started()), this-&gt;pObject, SLOT(fnc2()));

this-&gt;thread1-&gt;start();

//after thread1 has finished

this-&gt;pObject-&gt;moveToThread( this-&gt;thread2 );
this-&gt;thread2-&gt;start();
</code></pre>

<p>Is it possible to do this?</p>

<p>Edit: After Kuba's advice on not using a direct connection and him pointing out I must be interfering with the event loop somehow, I realised manually terminating the threads was not a good idea. I am adding my termination of the threads here to show where I am going wrong and to try and find a better way of achieving the same result.</p>

<pre><code>connect(this-&gt;pObject, SIGNAL(finished()), this, SLOT(stopThread()));

void Class::stopThread( void )
{
    if( this-&gt;thread1-&gt;isRunning() )
    {
        this-&gt;thread1-&gt;terminate();
        return;
    }

    if( this-&gt;thread2-&gt;isRunning() )
    {
        this-&gt;thread2-&gt;terminate();
        this-&gt;pObject-&gt;moveToThread( this-&gt;thread1 );
    }
}

void Object::fnc1( void )
{
    /*Does some work..*/

    finished(); //Calls 'finished' to signal stopThread when done (not stopping on its own)
}
</code></pre>

<p><em>ADDITIONAL INFO</em>
I have MainClass which holds the instances to both thread1, thread2 and pObject (pointer to the object I am trying to move from thread1 to thread2 and back again if necessary).</p>

<p>Main class constructor:</p>

<pre><code>MainClass::MainClass( QWidget *parent ) : QMainWindow(parent)
{
    this-&gt;ui.setupUi(this);

    this-&gt;thread1           = new QThread( this );
    this-&gt;thread2           = new QThread( this );

    this-&gt;pObject-&gt;moveToThread( this-&gt;thread1 );

    connect( this-&gt;pObject, SIGNAL(finished()), this, SLOT(stopThread()) );
    connect( this-&gt;thread1, SIGNAL(started()), this-&gt;pObject, SLOT(fnc1()) );
    connect( this-&gt;thread2, SIGNAL(started()), this-&gt;pObject, SLOT(fnc2()) );   
}
</code></pre>

<p>Slot for when menu item is clicked:</p>

<pre><code>void MainClass::on_action_call_fnc1_triggered( void )
{
    if( this-&gt;thread1-&gt;isRunning() )
        return;

    /*EXECUTES SOME CLASSIFIED CODE THAT CANNOT BE SHOWN*/

    this-&gt;thread1-&gt;start();//should trigger fnc1 execution
}
</code></pre>

<p>fnc1 held in Object class that is called when thread1 starts:</p>

<pre><code>void Object::fnc1( void )
{
    /*DOES SOME MORE SECRET PROCESSING THAT CANNOT BE SHOWN*/
    this-&gt;finished(); // triggers MainClass::stopThread( void )
}
</code></pre>

<p>Slot for when menu item is clicked to start fnc2 executing:</p>

<pre><code>void MainClass::on_action_call_fnc2_triggered( void )
{
    if( this-&gt;thread1-&gt;isRunning() || this-&gt;thread2-&gt;isRunning() )
        return;

    this-&gt;pObject-&gt;moveToThread( this-&gt;thread2 );
    this-&gt;thread2-&gt;start();//should trigger fnc2 execution
}
</code></pre>

<p>fnc2 held in Object:</p>

<pre><code>void Object::fnc2( void )
{
   /*DOES SOME MORE SECRET PROCESSING THAT CANNOT BE SHOWN*/
    this-&gt;finished(); // triggers MainClass::stopThread( void )
}
</code></pre>

<p>stopThread function:</p>

<pre><code>void MainClass::stopThread( void )
{
    if( this-&gt;thread1-&gt;isRunning() )
    {
        /*this-&gt;thread1-&gt;quit();
        this-&gt;thread1-&gt;exit();*/
        this-&gt;thread1-&gt;terminate();
        //this-&gt;thread1-&gt;wait( 0 ); //Trying different ways of stopping the thread
        return;
    }

    if( this-&gt;thread2-&gt;isRunning() )
    {
        this-&gt;thread2-&gt;terminate();
        //this-&gt;thread2-&gt;quit(); // Trying different ways of stopping the thread
        //this-&gt;thread2-&gt;exit();
        this-&gt;pObject-&gt;moveToThread( this-&gt;thread1 );
    }
}
</code></pre>