QSystemTrayIcon недоступен в приложении, если запускается с помощью sudo.Как решить эту проблему?
./qsystemtrayicontest - the tray icon is available
sudo ./qsystemtrayicontest - the tray icon is not available
output
QML debugging is enabled. Only use this in a safe environment.
QStandardPaths: XDG_RUNTIME_DIR not set, defaulting to '/tmp/runtime-root'
ERROR: No native SystemTrayIcon implementation available.
Qt Labs Platform requires Qt Widgets on this setup.
Add 'QT = widgets' to .pro and create QApplication in main().
qml: SystemTrayIcon::available false
Ниже приведен минимальный пример с интерфейсом qml.Но на обычном виджете ошибка повторяется таким же образом.
qsystemtrayicontest.pro
QT += quick
CONFIG += c++11
QT += widgets
DEFINES += QT_DEPRECATED_WARNINGS
SOURCES += \
main.cpp
RESOURCES += qml.qrc
QML_IMPORT_PATH =
QML_DESIGNER_IMPORT_PATH =
qnx: target.path = /tmp/$${TARGET}/bin
else: unix:!android: target.path = /opt/$${TARGET}/bin
!isEmpty(target.path): INSTALLS += target
main.cpp
#include <QGuiApplication>
#include <QQmlApplicationEngine>
int main(int argc, char *argv[])
{
QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
QGuiApplication app(argc, argv);
QQmlApplicationEngine engine;
engine.load(QUrl(QStringLiteral("qrc:/main.qml")));
if (engine.rootObjects().isEmpty())
return -1;
return app.exec();
}
main.qml
import QtQuick 2.12
import QtQuick.Window 2.12
import Qt.labs.platform 1.0
Window {
visible: true
width: 640
height: 480
title: qsTr("Hello World")
Text {
anchors.centerIn: parent
text: "SystemTrayIcon::available: " + idSystemTrayIcon.available
}
SystemTrayIcon {
id: idSystemTrayIcon
visible: true
Component.onCompleted: {
console.log("SystemTrayIcon::available", available)
}
}
}