图像未显示在与 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 运行程序时的输出

correct output

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...