简体   繁体   中英

How to check system tray availability programmatically?

Upon launching QSyncthingTray without any panel/trayer running, it displays an error dialog:

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

and exits upon pressing [ OK ] .

Curiously though, if I have QSyncthingTray already running as a tray icon and kill my panel, QSyncthingTray doesn't quit, and upon restarting the panel QSyncthingTray is still there...

For my ~/.xsession & ~/.xinitrc startup, I'd like to add a looped check whether a tray is present, pseudocode:

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

How do I do the if system-tray.is-available ?
The preferable language is Perl , also interested in Raku and POSIX sh . Other languages are welcome as well.

QSyncthingTray uses isSystemTrayAvailable() from Qt c++ lib.

You can do the same.

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 retarded and you need to use fullblown build system to build programs using it

is_tray_available.pro :

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

save both files in the same dir, execute qmake , make and voilà, you have executable that you can use in shellscript

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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