1
我必须管理由更多udp通道消息更新的MainWindow。此外,阅读配置文件,我必须显示或不主窗口,如果不管理/派遣/过滤udp消息到另一个应用程序。是否可以定义一个引擎类来管理通信并更新Mainwindow(如果显示)?我如何管理主窗口?也许使用单身? 我会做什么(伪代码):在工作线程中使用QT MainWindow
main.cpp中:
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
Engine app();
app.run();
return a.exec();
}
Engine.cpp
Engine::Engine(QObject *parent):
QObject(parent)
{
show_mainwindow = false;
thread = new QThread();
Worker = new Worker();
// Filter/Dispatch
Worker->moveToThread(thread);
connect(Worker, SIGNAL(error(QString)), this, SLOT(errorString(QString)));
connect(thread, SIGNAL(started()), Worker, SLOT(process()));
connect(Worker, SIGNAL(finished()), thread, SLOT(quit()));
connect(Worker, SIGNAL(finished()), Worker, SLOT(deleteLater()));
connect(thread, SIGNAL(finished()), thread, SLOT(deleteLater()));
thread->start();
// Message Receiver (more)
UdpRecvr = new Receiver();
connect(UdpRecvr, SIGNAL(receivedMsg(const QByteArray)), udpManager, SLOT(processMsg(const QByteArray)));
//Mainwindow Singleton used to update with SIGNAL/SLOT
WINDOWINSTANCE = MainWindow::GetInstance();
if(show_mainwindow)
{
WINDOWISTANCE->show();
}
}
此外,在UdpRecvr类我会用主窗口辛格尔顿。我读过使用带SIGNAL/SLOT的单身人士是线程安全的。这是一个适当的设计方案吗? 在此先感谢。
不建议使用线程工作者。如果使用基于任务的线程,则更好(检查QtConcurrent) – Apin