I wanted to know what is the best practice to connect signal/slots between two QObjects created in the contructor of MainWindow but moved to different threads later...default connections seems not working then when I connect with the option Qt::Directconnection
things start working...but sometimes the signal/slot fails...following is my code pattern..please let me know if I need to change my class design...
MainWindow.cpp
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
{
myObjectA = new ObjectA;
myObjectB = new ObjectB;
connect( myObjectA,SIGNAL(signalA()),myObjectB,SLOT(slotB()) );
myObjectA.initiateProcess();
myObjectB.initiateProcess();
}
ObjectA.h
#include <QThread>
#include <QObject>
class ObjectA : public QObject
{
Q_OBJECT
public:
explicit ObbjectA(QObject *parent = 0);
void inititateProcess();
public slots:
void do_job();
signals:
void signalA();
private:
QThread *worker;
}
ObjectA.cpp
ObjectA::ObjectA(QObject* parent)
{
....
}
void ObjectA::do_jobA()
{
//do something;
}
void ObjectA::initiateProcess()
{
worker = new QThread;
connect(worker,SIGNAL(started()),this,SLOT(do_jobA()));
this->moveTo(worker);
worker->start()
}
ObjectB.h
#include <QThread>
#include <QObject>
class ObjectB : public QObject
{
Q_OBJECT
public:
explicit ObjectB(QObject *parent = 0);
void initiateProcess();
public slots:
void do_job();
void slotB();
signals:
void signalB();//for some other slot
private:
QThread *worker;
}
ObjectB.cpp
ObjectB::ObjectB(QObject* parent)
{
....
}
void ObjectB::do_jobB()
{
//do something;
}
void ObjectB::initiateProcess()
{
worker = new QThread;
connect(worker,SIGNAL(started()),this,SLOT(do_jobB()));
this->moveTo(worker);
worker->start()
}