首页 > 解决方案 > 使用 windeployqt 工具后 QSystemTrayIcon 未显示

问题描述

注意:是的,这个问题之前已经提出并回答过,但重复的答案不完整。如果您对此感到困惑,请查看我的解决方案。

使用 Qt 5.13.0 MSVC2017 64 位和 Windows 10,我遇到了来自 QSystemTrayIcon 的一些奇怪行为。在全新构建的 QtCreator 中运行调试或发布时,没有任何问题,并且图标完全按照我的预期显示。但是,使用该windeployqt.exe工具后,图标停止显示。

当我从 QtCreator 运行它之后windeployqt.exe以及通过 Qt Installer Framework 从安装目录运行它时,都会发生这种情况。如果我删除构建目录,清理并重建,问题就会消失,但显然这是部署的问题。

命令:

windeployqt.exe <RELEASE_DIR>

(也尝试直接指定可执行文件)

MainWindow 构造函数:

AltheaWindow::AltheaWindow(MacroListModel &macroListModel, Config *config, const QString &configPath, QWidget *parent)
        : QMainWindow(parent),
          ui(new Ui::AltheaWindow),
          macros(macroListModel),
          config(config),
          trayIcon(QIcon(IconPath), this),
          shortcutNew(ShortcutPtr(new QShortcut(NewMacro, this))),
          shortcutSave(ShortcutPtr(new QShortcut(SaveMacro, this))) {
    qDebug() << "AltheaWindow";
    ui->setupUi(this);
    ui->sidebar->createSettingsDialog(config, configPath);
    initConnections();
    QFile f(IconPath);
    bool exists = f.exists();
    qDebug() << "exists = " << exists; // "exists = true"
    trayIcon.show();
    ui->sidebar->setMacros(&macroListModel);
    if ((*(*config)[AltheaConfig::StartMinimized]).toBool()) showMinimized();
}

无论是否显示图标,exists总是true,因此似乎不是解决资源的问题。应用程序中的所有其他资源也可以正常工作。

标签: c++windowsqt

解决方案


找到解决方案,将图像格式复制到插件目录。

解决方案:https ://stackoverflow.com/a/20594583/2472874

附加说明:对于遇到此问题的任何人,我发现在初始化应用程序对象之前,我还需要在库路径上安装 imageformats 插件。

#define PLUGINS "plugins"

void preInit(char *argv[]);

int main(int argc, char *argv[]) {
#ifndef _DEBUG
    preInit(argv);
#endif
    Althea app(argc, argv);
    AltheaWindow w(app.getMacros(), &app.getConfig(), app.getAppData().absoluteFilePath(Althea::ConfigFileName));
    w.show();
    return app.exec();
}

void preInit(char *argv[]) {
    QDir releaseDir(QFileInfo(argv[0]).dir());
    QCoreApplication::addLibraryPath(releaseDir.absoluteFilePath(PLUGINS));
}

推荐阅读