c++ - 使用 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 ¯oListModel, 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(¯oListModel);
if ((*(*config)[AltheaConfig::StartMinimized]).toBool()) showMinimized();
}
无论是否显示图标,exists
总是true
,因此似乎不是解决资源的问题。应用程序中的所有其他资源也可以正常工作。
解决方案
找到解决方案,将图像格式复制到插件目录。
解决方案: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));
}
推荐阅读
- html - 使用 bulma 在图像两侧环绕文本
- java - 在 Kstream Spring kafka 应用程序中丢弃消息
- reactjs - 如何部署 Electron React 样板应用程序?
- python - 遍历字典中的多个键和项值
- github - 设置本地动作脚本
- java - mockMvc .contentType(MediaType.APPLICATION_JSON) 的目的是什么?
- javascript - KeyboardEvent:按下“Shift”时如何获取数字键代码?
- nlog - 带有 ${oneexception} 的 NLog 布局不显示文字文本
- fonts - Vuepress 添加字体
- linux - linux和openai健身房问题