如何解决图像未显示在与 linuxdeployqt
我在 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"
}
}
}
main.cpp
#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 运行程序时的输出。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。