简体   繁体   English

如何使用 Qt/c++ 为所有 UNIX 操作系统创建托盘图标?

[英]How do I create a trayicon with Qt/c++ for all UNIX OS?

I'm working with Qt/C++ creating my own app, I want it to be cross-platform, so far everything it's been working great with Qt, the only problem its with the tray-icon.我正在使用 Qt/C++ 创建我自己的应用程序,我希望它是跨平台的,到目前为止,它在 Qt 上运行良好,唯一的问题是托盘图标。

I create a tray-icon and add a menu to it, this works fine in windows but it's not working in Linux.我创建了一个托盘图标并向其添加了一个菜单,这在 Windows 中运行良好,但在 Linux 中不起作用。 I test my app in two computers and got two different respond.我在两台计算机上测试了我的应用程序,得到了两种不同的响应。

In my desktop (Ubuntu 14.04) the tray-Icon appears on the right side of the task-bar as expected but it doesn't show me the menu.在我的桌面(Ubuntu 14.04)中,托盘图标按预期出现在任务栏的右侧,但没有显示菜单。 And in the laptop (also Ubuntu 14.04) the tray-icon appears on the left side of the task-bar but in this case it does shows me the menu and a message when I double click the icon.在笔记本电脑(也是 Ubuntu 14.04)中,托盘图标出现在任务栏的左侧,但在这种情况下,当我双击该图标时,它确实向我显示了菜单和一条消息。

I don't know if there's another way to do this but here it's my code.我不知道是否还有其他方法可以做到这一点,但这是我的代码。

if (QSystemTrayIcon::isSystemTrayAvailable())
{
  //trayicon
  trayIcon = new QSystemTrayIcon(this);
  trayIcon->setIcon(QIcon(":/Imagenes/iconosPERFQ-23.png"));
  trayIcon->setToolTip(tr("PerfQ Client"));
  trayIcon->setVisible(true);

  connect(trayIcon, SIGNAL(activated(QSystemTrayIcon::ActivationReason)),
                 this, SLOT(iconActivated(QSystemTrayIcon::ActivationReason)));
  //Menu
  logoutAction = new QAction(tr("&Logout"), this);
  trayIconMenu = new QMenu(this);
  trayIconMenu->addAction(logoutAction);
  // Add the menu to the trayicon
  trayIcon->setContextMenu(trayIconMenu);

  connect (logoutAction,SIGNAL(triggered()), this, SLOT(logout()));
}

and the slot for the activated signal on the trayicon以及托盘图标上激活信号的插槽

void Task::iconActivated(QSystemTrayIcon::ActivationReason reason)
{
  switch (reason)
  {
    case QSystemTrayIcon::DoubleClick:
      QMessageBox::information(this,"Double Click", "Double click has been press on trayicon");
      break;
    default:
      ;
  }
}

The code's not the fault - Ubuntu changed the system notification few years ago from sni (status notifier indicator) to appnotifier which is working via D-Bus and is not backwards compatible with sni.代码不是问题 - 几年前,Ubuntu 将系统通知从 sni(状态通知程序指示器)更改为通过 D-Bus 工作且不向后兼容 sni 的 appnotifier。

Though, there is a package in Ubuntu - sni-qt and sni-qt:386 - which helps to show systray icons for applications built with Qt prior to 5.xx version.不过,在 Ubuntu 中有一个包 - sni-qt 和 sni-qt:386 - 它有助于显示使用 Qt 5.xx 版本之前构建的应用程序的系统托盘图标。

Also, if you're building with Qt 5.xx - upgrade Qt to 5.5.此外,如果您使用 Qt 5.xx 构建 - 将 Qt 升级到 5.5。 It is working with the new appindicator via D-Bus.它正在通过 D-Bus 与新的 appindicator 一起工作。

There is no QSystemTrayIcon::activated on Ubuntu. Ubuntu 上没有QSystemTrayIcon::activated The only thing that happens when you (single) click the icon is that the context menu opens.当您(单次)单击该图标时,唯一会发生的事情是上下文菜单打开。

This is a fact you have to deal with, a UI choice borrowed from OS X. It dramatically simplifies the interaction with tray icons, because there is no single/double/right click which every application uses differently.这是一个你必须处理的事实,一个从 OS X 借来的 UI 选择。它极大地简化了与托盘图标的交互,因为没有每个应用程序使用不同的单击/双击/右键单击。 Just the menu that does everything.只是做所有事情的菜单。

Regarding your different behavior: could you check those Ubuntu versions again?关于你的不同行为:你能再检查一下那些 Ubuntu 版本吗? It looks like the differences came with the transition from Gnome to Unity.看起来差异是随着从 Gnome 到 Unity 的过渡而来的。 The Gnome way of dealing with tray icons differs in many points from what a current Ubuntu 15.04 is doing in Unity. Gnome 处理托盘图标的方式在很多方面与当前的 Ubuntu 15.04 在 Unity 中所做的不同。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM