在不同的线程的对象之间的槽基本上,我已经下一个代码:信号和Qt的
class serverTCP : public QObject
{
// Other methods...
signals:
void newInstructions(QJsonDocument jDoc);
public slots:
void responseObtained(QJsonDocument jDoc);
}
class gatherModbus : public QObject
{
// Other methods...
signals:
void responseReady(QJsonDocument jDoc);
public slots:
void executeInstruction(QJsonDocument jDoc);
void myprocess() {
while(true)
{
// Write and read Serial Port
// Save data in json
}
};
}
void main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
int netsNumber = 2; //In reality this is dynamic. It's only a example
serverTCP *server = new serverTCP;
gatherModbus * gather = new gatherModbus[netsNumber];
QThread * threads = new QThread[netsNumber];
// more necessary code...
for(int i = 0; i < netsNumber; i++)
{
gather[i].moveToThread(threads[i]);
QObject::connect(&server, SIGNAL(newInstructions(QJsonDocument)),
&gather[i], SLOT(executeInstruction(QJsonDocument)));
QObject::connect(&gather[i], SIGNAL(responseReady(QJsonDocument)),
&server, SLOT(responseObtained(QJsonDocument)));
QObject::connect(&threads[i], SIGNAL(start()),
&gather[i], SLOT(myprocess()));
// Other signals needed between the objects 'gather' and 'threads'
threads[i].start();
}
a.exec();
}
的问题是,对象“服务器”之间的连接'聚到一不工作。对象'server'和'main'函数在同一个线程中,但'gather'对象已经移动到其他线程中。
我该怎么做才能使两个对象能够正确通信?
我的目的是'服务器'必须能够发送信号到所有对象'聚集'有。在每个对象的“聚集”中,必须执行该槽并返回“服务器”响应(如果有的话)。
如果我将连接设置为Qt::DirectConnection
类型的插槽,它与'main'函数和对象'server'在同一线程上运行,并且这些插槽对我没有兴趣。
任何帮助或建议,将不胜感激。
我看不到'void myprocess()'在哪里调用。如果不是,请从代码示例中删除 – UmNyobe
谢谢!我忘记将连接启动信号线程设置为“myprocess”方法。 – Jonathan