如何以编程方式检测系统托盘的可用性?

4

如果没有面板或托盘运行,启动QSyncthingTray将显示一个错误对话框:

I couldn't detect any system tray on this system.
                                         [ OK ]

[ OK ]键即可退出。

有趣的是,如果我已经将QSyncthingTray图标显示在系统托盘中,然后关闭面板,QSyncthingTray不会退出。重新启动面板后,QSyncthingTray仍然存在...

对于我的~/.xsession~/.xinitrc启动文件,我想添加一个循环检查系统托盘是否存在的伪代码:

loop:
  if system-tray.is-available:
    run QSyncthingTray
    done
  else sleep 1

我该如何实现if system-tray.is-available的功能呢? 首选语言是Perl,也有兴趣尝试Raku和POSIX的sh。 其他编程语言也可以。
1个回答

3

QSyncthingTray 使用 Qt C++ 库中的 isSystemTrayAvailable()

你也可以这样做。

is_tray_available.cpp:

// program that exits with zero code when tray is available
// when not available (or program crashes) it should return non-zero

#include <QApplication>
#include <QSystemTrayIcon>

int main(int argc, char** argv)
{
  QApplication app(argc, argv);
  if(QSystemTrayIcon::isSystemTrayAvailable()) return 0;
  else return 1;
}

QT 在编写程序时需要使用完整的构建系统进行构建,这是它独特的地方:

is_tray_available.pro:

QT += widgets
TARGET = is_tray_available
SOURCES += is_tray_available.cpp

将两个文件保存在同一个目录中,执行qmakemake,然后你就有了一个可用于shell脚本的可执行文件。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接