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
結果の large_qrc-x86_64.AppImage 実行可能ファイルを Ubuntu GUI からダブルクリックできます。アプリケーションは正常に起動しますが、上部の画像が表示されません。
QML Image が .qrc のイメージにアクセスできるようにするには、何を変更する必要がありますか?
2 つ目の質問は、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 からプログラムを実行したときです。