我已将QSystemTrayIcon :: ActivationReason连接到我的'handleClick'插槽,如下所示
connect(tray,SIGNAL(activated(QSystemTrayIcon::ActivationReason)), this,SLOT(handleClick(QSystemTrayIcon::ActivationReason)));
mywindow::handleClick(QSystemTrayIcon::ActivationReason reason)
{
switch (reason)
{
case QSystemTrayIcon::Trigger:
case QSystemTrayIcon::DoubleClick:
handleLeftClickOnTray();
break;
case QSystemTrayIcon::MiddleClick:
break;
default:;
}
}
我有另一个名为'handleRightClickOnSystemTray()'的函数,当鼠标右键单击系统托盘图标时应调用该函数。此功能创建一个QDialog框并显示它。如何处理左键单击系统托盘?
答案 0 :(得分:1)
您可以使用左键单击时调用的QSystemTrayIcon::Trigger。当菜单到位时,QSystemTrayIcon::DoubleClick不会触发。
case QSystemTrayIcon::Trigger:
qDebug() << "Left clicked";
break;
如果这对您不起作用,则必须重新实现bool QSystemTrayIcon::event(QEvent *e)并安装事件过滤器以检查按下了哪个按钮。
新信号和插槽语法是
connect(tray, &QSystemTrayIcoon::activated, this, &MainWindow::handleClick /* assuming it's MainWindow */);
或lamba版
connect(tray, &QSystemTrayIcoon::activated, [](QSystemTrayIcon::ActivationReason reason) { switch(reason) { /* .. */ } });