[英]System tray icon is not responsive
When I open my application then the application is waiting for a connection to the server, I have done that by calling a slot run()
which waits for a acknowledgement packet from server and when it receives it then it hides "Waiting for connection" string and loads other things. 当我打开我的应用程序然后应用程序正在等待连接到服务器时,我已经通过调用插槽run()
等待来自服务器的确认数据包并且当它接收它然后它隐藏“等待连接”字符串并加载其他东西。 The problem is that when it waits for a packet then the system tray icon is not responding to anything, when the server sends packet and application loads then the system tray icon starts responding (for right-click menu). 问题是当它等待数据包时系统托盘图标没有响应任何东西,当服务器发送数据包和应用程序加载时,系统托盘图标开始响应(对于右键单击菜单)。
I am using ZeroMQ for IPC. 我正在使用ZeroMQ进行IPC。
I have something like this: 我有这样的事情:
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
//THIS PART
QTimer::singleShot(2000,&w,SLOT(run()));
return a.exec();
}
You block the event loop. 您阻止事件循环。 This won't ever work. 这将永远不会奏效。 Unfortunately, ZMQ doesn't offer any platform-specific message loop integration. 不幸的是,ZMQ不提供任何特定于平台的消息循环集成。 Thus you have to use it in a separate thread. 因此,您必须在单独的线程中使用它。
This is much easier since it's bad design anyway to put networking code in a widget class. 这更容易,因为无论如何将网络代码放入窗口小部件类中都是糟糕的设计。
Create a ZMQ
object that encapsulates your networking, and push it to a separate thread. 创建一个封装网络的ZMQ
对象,并将其推送到单独的线程。 As long as all your communication with that ZMQ instance is over signals/slots or QMetaObject::invokeMethod
, you'll be OK. 只要你与ZMQ实例的所有通信都是通过信号/槽或QMetaObject::invokeMethod
,你就可以了。
See this answer for code to Thread
. 有关Thread
代码,请参阅此答案 。
class ZMQ : public QObject {
Q_OBJECT
Q_SLOT void run() {
...
forever {
socket.send(request,0);
socket.recv(&response);
if(response.compare("login") == 0) {
emit loggedIn();
socket.close();
return;
}
}
}
public:
ZMQ() {}
Q_SIGNAL void loggedIn();
};
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
ZMQ zmq;
Thread thread; // see https://stackoverflow.com/a/25230470/1329652
MainWindow w;
w.connect(&zmq, SIGNAL(loggedIn()), SLOT(loggedIn()));
zmq.moveToThread(&thread);
thread.start();
QMetaObject::invokeMethod(&zmq, "run");
w.show();
return a.exec();
}
You need to run the code currently in Mainwindow::run() in a separate thread for it to not block the event loop. 您需要在单独的线程中运行当前Mainwindow :: run()中的代码,以便不阻止事件循环。
Make a new QObject with a run slot containing the same code and move it to its own thread. 使用包含相同代码的运行槽创建一个新的QObject,并将其移动到自己的线程。 Something like this: 像这样的东西:
QThread *thread = new QThread;
thread->start();
qobject_with_run_slot->moveToThread(thread);
QTimer::singleShot(2000, qobject_with_run_slot, SLOT(run()));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.