i am new with QThreads. And didn't understand why i get no new Thread Id for my class. But first a few basic about my problem.
I have a normal c++ class which need a long time to compute lets name her HeavyBaseClass. In this class i have no QT elements.
My goal is to compute this class in her own thread so that the GUI can run normal when she is computing.
I read a few implementation for Qt threading problem and I decided to use QTthread with an QObject. Becuase it should be possible to stop the thread and with my first try QtConcurrent this was not possible.
Because that i created my own QObject:
h.file:
#include <QObject>
#include <HeavyBaseClass.h>
class Qoptimization : public QObject , HeavyBaseClass
{
Q_OBJECT
public:
Qoptimization(HeavyBaseClass INPUT PARAMETER,QObject *parent = 0);
void debugThread();
signals:
public slots:
};
#endif // QOPTIMIZATION_H
cpp file:
#include "qoptimization.h"
#include <QDebug>
#include <QThread>
Qoptimization::Qoptimization(HeavyBaseClass INPUT PARAMETER, QObject *parent) :
QObject(parent),HeavyBaseClass (INPUT PARAMETER)
{
qDebug() << " init Thread " << this->thread()->currentThreadId() ;
}
void Qoptimization::debugThread(){
qDebug() << " current Thread " << thread()->currentThreadId() ;
}
now i call this on my mainwindow
qDebug() << " Main Thread " << this->thread()->currentThreadId();
Qoptimization qoptimization(f);
QThread *optimizationThread = new QThread();
qDebug() << " before moving to Thread " << qoptimization.thread()->currentThreadId();
qoptimization.moveToThread(optimizationThread);
optimizationThread->start();
qoptimization.debugThread();
qDebug() << " after moving Thread " << qoptimization.thread()->currentThreadId();
the problem which i dont understand is that my thead id is the same on alle outputs.
DEBUG Information:
Main Thread 0xd88
init Thread 0xd88
before moving to Thread 0xd88
current Thread 0xd88
after moving Thread 0xd88
Do you have an idea what I did wrong ?
UPDATE:
new cpp
void Qoptimization::debugThread(){
qDebug() << " in debugThred currentThread() " << QThread::currentThread() ;
qDebug() << " in debugThread Thread " << thread() ;
if ( QThread::currentThread() != thread() )
{
// Force slot to be emmited in object thread
QTimer::singleShot( 0, this, SLOT( debugThread() ) );
return ;
}
}
Output:
Main Thread 0x156c
init Thread 0x156c
before moving to Thread 0x156c
in debugThred currentThread() QThread(0xeccc00)
in debugThread Thread QThread(0x24cad80)
after moving Thread 0x156c