QSystemTrayIcon不可用,然后在Ubuntu 18.04 LTS上使用sudo运行应用程序

时间:2019-04-23 14:17:21

标签: qt ubuntu qml sudo

如果使用sudo运行,

QSystemTrayIcon在应用程序中不可用。该问题如何解决?

./qsystemtrayicontest        - the tray icon is available
sudo ./qsystemtrayicontest   - the tray icon is not available

输出

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)
        }
    }
}

0 个答案:

没有答案