简体   繁体   English

系统托盘应用程序Linux Qt / C ++

[英]System tray application Linux Qt/C++

I'm writing an application in C++ with Qt that utilizes the system tray. 我正在使用Qt编写一个使用系统托盘的C ++应用程序。 I have implemented the system tray using a QSystemTrayIcon class as shown in the examples, but it doesn't have the same behavior as other system tray icons that are present on my computer. 我已使用QSystemTrayIcon类实现了系统托盘,如示例所示,但它与我的计算机上存在的其他系统托盘图标的行为不同。 For instance, I have Spotify installed on Ubuntu 12.04 and it shows a system tray icon with a drop down menu. 例如,我在Ubuntu 12.04上安装了Spotify,它显示了一个带有下拉菜单的系统托盘图标。 With my application, it shows a system tray icon with a context menu, meaning you have to right click it to make the menu active. 使用我的应用程序,它会显示带有上下文菜单的系统托盘图标,这意味着您必须右键单击它才能激活菜单。 With Spotify, all that needs to be done is to click on the icon and the menu will show. 使用Spotify,所有需要做的就是点击图标,菜单就会显示出来。 What can I do to get native system tray icons in Ubuntu? 如何在Ubuntu中获取本机系统托盘图标? I'm fine with using specific code for X11/Linux and not the built-in Qt functions. 我可以使用X11 / Linux的特定代码,而不是内置的Qt函数。 Thanks much. 非常感谢。

Here's my code: 这是我的代码:

void MainWindow::closeEvent(QCloseEvent *event)
{
    if (trayIcon->isVisible()) {
        hide();
        event->ignore();
    }
}

void MainWindow::createActions()
{
    restoreAction = new QAction(tr("&Show"), this);
    connect(restoreAction, SIGNAL(triggered()), this, SLOT(show()));

    quitAction = new QAction(tr("&Exit"), this);
    connect(quitAction, SIGNAL(triggered()), qApp, SLOT(quit()));
}

void MainWindow::createTrayIcon()
{
    trayIconMenu = new QMenu(this);
    accountsMenu = trayIconMenu->addMenu(tr("Accounts"));
    trayIconMenu->addSeparator();
    trayIconMenu->addAction(restoreAction);
    trayIconMenu->addSeparator();
    trayIconMenu->addAction(quitAction);

    trayIcon = new QSystemTrayIcon(this);
    trayIcon->setContextMenu(trayIconMenu);
}

Try to drop down menu from activated signal of QSystemTrayIcon. 尝试从QSystemTrayIcon的激活信号下拉菜单。

void Window::iconActivated(QSystemTrayIcon::ActivationReason reason)
 {
     switch (reason) {
         case QSystemTrayIcon::Trigger:
             // show your menu here
     }
 }

I am commenting for the benefit of others here... I had the same issue when we deployed our product (built using Qt)on Ubuntu 12.04 LTS. 我在这里评论其他人的好处...当我们在Ubuntu 12.04 LTS上部署我们的产品(使用Qt构建)时,我遇到了同样的问题。 We use the qt.conf way of deployment. 我们使用qt.conf部署方式。 After a lot of hunting and going through the source on sni-qt I found that plugins need to be properly found out. 经过大量的狩猎并经过sni-qt的源代码后,我发现需要正确找到插件。 So I created and copied the plugins from our build environment to the plugins directory relative to my application path mentioned in qt.conf against the 'Plugins = ' entry. 所以我创建并将插件从我们的构建环境复制到插件目录,相对于我在qt.conf中提到的针对'Plugins ='条目的应用程序路径。 Also made sure that sni-qt is update and installed on the deployed Ubuntu 12.04 machine. 还要确保在部署的Ubuntu 12.04计算机上更新并安装了sni-qt。 The menus appeared as they appear for other tray applications. 菜单显示为其他托盘应用程序。 You can copy plugins from /usr/lib/i386-linux-gnu/qt4/plugins/ on a 32 bit machine or its equivalent path on 64 bit machine. 您可以在32位计算机或64位计算机上的等效路径上复制/ usr / lib / i386-linux-gnu / qt4 / plugins /中的插件。 For this problem plugin under systemtrayicon is the required one. 对于这个问题,systemtrayicon下的插件是必需的。

HTH. HTH。

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

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