我在https://github.com/jh3010-qt-questions/large_qrc有一个示例项目
如果我从 Qt Creator 运行应用程序,我会得到一个如下所示的窗口。
但是,如果我使用 linuxdeployqt 打包应用程序,则仅显示窗口底部的图像。它直接从磁盘加载。上面的图像来自一个应该附加到可执行文件的 .qrc,但它没有出现。
我的目录结构遵循 linuxdeployqt 的建议
$ tree usr
usr
├── bin
│ ├── image_assets
│ │ └── original_image_png
│ └── large_qrc
├── lib
└── share
├── applications
│ └── large_qrc.desktop
└── icons
└── hicolor
└── 256z256
└── apps
└── large_qrc.png
我通过执行以下操作构建应用程序映像:
linuxdeployqt usr/share/applications/large_qrc.desktop -appimage -qmake=/home/jamesh/Qt5.12.10/5.12.10/gcc_64/bin/qmake -qmldir=/home/jamesh/depot_qt/questions/large_qrc
我可以从 Ubuntu GUI 双击生成的 large_qrc-x86_64.AppImage 可执行文件。应用程序成功启动,但顶部图像未显示。
需要更改什么以便 QML Image 可以访问 .qrc 中的图像?
第二个问题是是否有更好的方法来处理 image_assets 中的图像。将它们复制到 bin 文件夹中似乎有点奇怪,但在这种情况下,这可能是最佳实践。
如果重要的话,我使用的是 Ubuntu 20.04
我的项目文件是:
QT += quick
CONFIG += c++11
image_assets.files = $$PWD/image_assets
image_assets.path = $$OUT_PWD
COPIES += image_assets
SOURCES += \
main.cpp
RESOURCES += qml.qrc
# Additional import path used to resolve QML modules in Qt Creator's code model
QML_IMPORT_PATH =
# Additional import path used to resolve QML modules just for Qt Quick Designer
QML_DESIGNER_IMPORT_PATH =
# Default rules for deployment.
qnx: target.path = /tmp/$${TARGET}/bin
else: unix:!android: target.path = /opt/$${TARGET}/bin
!isEmpty(target.path): INSTALLS += target
qml.qrc
<RCC>
<qresource prefix="/">
<file>main.qml</file>
<file>drawing.svg</file>
</qresource>
</RCC>
main.qml
import QtQuick 2.12
import QtQuick.Window 2.12
Window {
width: 640
height: 480
visible: true
title: qsTr("Hello World")
Column {
Image {
width: 200
height: 200
fillMode: Image.PreserveAspectFit
source: "qrc:drawing.svg"
}
Image {
width: 200
height: 200
fillMode: Image.PreserveAspectFit
source: "file:///" + applicationDirPath + "/image_assets/original_image_png"
}
}
}
主文件
#include <QGuiApplication>
#include <QQmlApplicationEngine>
#include <QQmlContext>
int main(int argc, char *argv[])
{
QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
QGuiApplication app(argc, argv);
QQmlApplicationEngine engine;
engine.rootContext()->setContextProperty( "applicationDirPath", QGuiApplication::applicationDirPath() );
const QUrl url(QStringLiteral("qrc:/main.qml"));
QObject::connect(&engine, &QQmlApplicationEngine::objectCreated,
&app, [url](QObject *obj, const QUrl &objUrl) {
if (!obj && url == objUrl)
QCoreApplication::exit(-1);
}, Qt::QueuedConnection);
engine.load(url);
return app.exec();
}
large_qrc.desktop
[Desktop Entry]
Type=Application
Name=large_qrc
Comment=The best Qt Application Ever
Exec=large_qrc
Icon=large_qrc
Categories=Office;
这应该是输出,当我从 Qt Creator 运行程序时。