我想知道在 MainWindow 的构造器中创建的两个 QObjects 之间连接信号/插槽的最佳做法是什么,但后来移到不同的线程...默认连接似乎不起作用,然后当我使用选项连接时,Qt::Directconnection
事情开始工作...但有时信号/插槽失败...以下是我的代码模式..如果我需要更改我的类设计,请告诉我...
主窗口.cpp
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
{
myObjectA = new ObjectA;
myObjectB = new ObjectB;
connect( myObjectA,SIGNAL(signalA()),myObjectB,SLOT(slotB()) );
myObjectA.initiateProcess();
myObjectB.initiateProcess();
}
对象A.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;
}
对象A.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()
}
对象B.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;
}
对象B.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()
}