Linux平台下用Qt开发的托盘程序,用root权限运行程序时,托盘图标不显示

在linux下平台下开发一个托盘程序,在普通用户下托盘图标可以显示,在root权限时,托盘图标不显示

以下为运行demo代码

#include
#include
#include

#include "networkmanager.h"

int main(int argc, char *argv[])
{
QApplication::setApplicationName("Demo1");
QApplication app(argc, argv);

 if (!QSystemTrayIcon::isSystemTrayAvailable())
 {
     QMessageBox::critical(0, QObject::tr("Systray"),
                           QObject::tr("I couldn't detect any system tray "
                                       "on this system."));
     return 1;
 }

 QApplication::setQuitOnLastWindowClosed(false);

 QSystemTrayIcon demo1;

 demo1.setIcon(QIcon(":/new/prefix1/resource/ox32-device-network-defaultroute.png"));

 demo1.show();

 return app.exec();

}

运行结果及报错内容

su root
./demo1
运行之后报如下错误,并没有托盘显示。

QStandardPaths: wrong ownership on runtime directory /var/run/user/1000, 1000 instead of 0
QStandardPaths: wrong ownership on runtime directory /var/run/user/1000, 1000 instead of 0

使用sudo ./demo1运行时报如下错误
QStandardPaths: XDG_RUNTIME_DIR not set, defaulting to '/tmp/runtime-root'

我想要达到的结果

已root权限运行托盘程序

  1. 以root权限运行GUI程序有安全隐患,一般不建议以root权限运行GUI程序
  2. Linux系统可能默认禁止root访问X server桌面服务

这个想法就很怪...用户A运行的程序显示在用户B的托盘图标里,这不是乱套嘛

我建议是Qt内做一个窗口提权,让用户在运行软件时输入管理员密码

装crossover

在/etc/profile末尾增加两句

export XDG_RUNTIME_DIR=/usr/lib/
export RUNLEVEL=3

然后刷新全局变量

source /etc/profile

不要使用root用户
安装root脚本,可以在脚本里加上sudo

https://blog.csdn.net/han3ying1jun8/article/details/84904423