简体   繁体   English

Qt - 如何检测应用程序是否在GNOME或KDE上运行?

[英]Qt - how to detect whether the application is running on GNOME or KDE?

I was wondering how I could do something like this ( source ) using Qt. 我想知道如何使用Qt做这样的事情( 来源 )。 I looked through the documentation but couldn't find any method to check if an external process is running. 我查看了文档,但找不到任何方法来检查外部进程是否正在运行。

if [ "$(pidof ksmserver)" ]; then
   echo "KDE running."
   # KDE-specific stuff here
elif [ "$(pidof gnome-session)" ]; then
   echo "GNOME running."
   # GNOME-specific stuff here
elif [ "$(pidof xfce-mcs-manage)" ]; then
   echo "Xfce running."
   # Xfce-specific stuff here
fi

Normally you shouldn't do this. 通常你不应该这样做。 Generally, if your application behaves differently depending on desktop environment, that will be a nasty surprise for any user that switches between them. 通常,如果您的应用程序根据桌面环境的不同行为,那对于在它们之间切换的任何用户来说都是一个令人讨厌的惊喜。

The alternative 替代方案

Use DE-agnostic commands like xdg-open . 使用与xdg-open等DE不相关的命令。 Advantages: 好处:

  • You don't have to write the logic yourself (xdg-utils already has done this) 你不必自己编写逻辑(xdg-utils已经完成了这个)
  • More user-friendly. 更加人性化。 It follows the user's actual preferences; 它遵循用户的实际偏好; many users use one DE but prefer some applications from a different DE. 许多用户使用一个DE但更喜欢来自不同DE的一些应用程序。
  • Supports other DEs like XFCE, LXDE, Unity, etc.. 支持其他DE,如XFCE,LXDE,Unity等。

For example, instead of opening a URL in Firefox or Konqueror according to the currently-running DE, pass the URL to xdg-open to open it in the user's preferred application. 例如,根据当前运行的DE,不是在Firefox或Konqueror中打开URL,而是将URL传递给xdg-open以在用户的​​首选应用程序中打开它。 (The user might be a Chromium user.) Don't hard-code nautilus or dolphin for GNOME and KDE; (用户可能是Chromium用户。)不要为GNOME和KDE硬编码nautilusdolphin ; instead open the path using xdg-open . 而是使用xdg-open路径。

Similarly, for other forms of interaction with the DE, try to use Freedesktop specifications , rather than trying to guess what DE is running. 同样,对于与DE的其他形式的交互,尝试使用Freedesktop规范 ,而不是试图猜测DE正在运行什么。 Standards exist for moving files to the trash , adding system tray applets, and adding files to the Recent Files list, among others. 存在将文件移动到废纸篓 ,添加系统托盘小程序以及将文件添加到“最近的文件”列表等的标准。

Use QProcess to run pidof foo , then check its stdout? 使用QProcess运行pidof foo ,然后检查它的标准输出? If this is not what you want, search /proc/ . 如果这不是你想要的,搜索/proc/

I believe the correct way to do what pidof does is to look at entries in /proc. 我相信做pidof的正确方法是查看/ proc中的条目。 There's another thread on this here: Find PID of a Process by Name without Using popen() or system() 这里还有另一个线程: 按名称查找进程的PID而不使用popen()或system()

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

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