C++ Can';t在Raspberry Pi上的QML图像中加载图像源

C++ Can';t在Raspberry Pi上的QML图像中加载图像源,c++,qt,raspberry-pi,qml,directory-structure,C++,Qt,Raspberry Pi,Qml,Directory Structure,我正在使用Qt Creator交叉编译Qt项目,出现以下错误: qrc:/main.qml:26:25: QML Image: Cannot open: qrc:/images/imgA.png 目标设备上的目录结构为: . └── ProjectA/ └── bin/ └── res/ └── images/ ├── imgA.png └── imgB.png 主机PC上的目录结

我正在使用Qt Creator交叉编译Qt项目,出现以下错误:

qrc:/main.qml:26:25: QML Image: Cannot open: qrc:/images/imgA.png
目标设备上的目录结构为:

.
└── ProjectA/
    └── bin/
        └── res/
            └── images/
                ├── imgA.png
                └── imgB.png
主机PC上的目录结构为:

.
└── ProjectA/
    ├── ProjectA.pro
    ├── src/
    │   └── All source Files
    └── res/
        ├── main.qml
        ├── main.qrc
        └── images/
            ├── imgA.png
            └── imgB.png
当我在主机PC上编译和执行程序时,一切正常

编辑 以下是main.qrc:

<RCC>
    <qresource prefix="/">
        <file>main.qml</file>
    </qresource>
    <qresource prefix="/images">
        <file alias="imgA" >images/imgA.png</file>
        <file alias="imgB" >images/imgB.png</file>
    </qresource>
</RCC>
下面是qmake项目文件:

QT += quick core network

CONFIG += c++17

# You can also make your code fail to compile if it uses deprecated APIs.
# In order to do so, uncomment the following line.
# You can also select to disable deprecated APIs only up to a certain version of Qt.
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000    # disables all the APIs deprecated before Qt 6.0.0
INCLUDEPATH += src/
SOURCES += \
        src/Property.cpp \
        src/Weather.cpp \
        src/main.cpp

RESOURCES += res/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 =


install_config.path = /home/dietpi/QtProjects/$${TARGET}/bin/Data/
install_config.files = Data/*


INSTALLS += \
    install_config \

# Default rules for deployment.
target.path = /home/dietpi/QtProjects/$${TARGET}/bin
!isEmpty(target.path): INSTALLS += target

HEADERS += \
    src/Property.hpp \
    src/Weather.hpp

DISTFILES += \
    res/main.qml \

这是为你剪下的代码

qml.qrc

<RCC>
    <qresource prefix="/">
    <file>main.qml</file>
    </qresource>

    <qresource prefix="/images">
    <file alias="logo.jpg">images/logo.jpg</file>
    </qresource>
</RCC>

这是为你剪下的代码

qml.qrc

<RCC>
    <qresource prefix="/">
    <file>main.qml</file>
    </qresource>

    <qresource prefix="/images">
    <file alias="logo.jpg">images/logo.jpg</file>
    </qresource>
</RCC>
如中所述,您应该使用
qrc:///path
:/path

似乎
qrc:/path
在某些平台上不起作用

更新:

由于您正在
.main.qrc
中使用
qresource前缀
,因此需要使用
:/images/images/
图像文件路径。或者将所有图像移动到父文件夹(
res/
)。

如中所述,您应该使用
qrc:///path
:/path

似乎
qrc:/path
在某些平台上不起作用

更新:


由于您正在
.main.qrc
中使用
qresource前缀
,因此需要使用
:/images/images/
图像文件路径。或者将所有图像移动到父文件夹(
res/
)。

查看资源文件时,您使用的是别名。这意味着您在引用它时必须使用别名。因此,请尝试这样访问它:

    Image {
        id: testImg
        source: "qrc:/images/imgA"    // Note, there is no '.png'
    }

查看资源文件时,您使用的是别名。这意味着您在引用它时必须使用别名。因此,请尝试这样访问它:

    Image {
        id: testImg
        source: "qrc:/images/imgA"    // Note, there is no '.png'
    }

我遇到了同样令人讨厌的问题,即使在不同的PC上安装了相同的Qt。当我在qrc文件中的每个前缀中添加最后一个斜杠时,效果很好:

<qresource prefix="/images/">


在那之后,我删除了缓存的所有.qrc并重新编译,它工作得很好。

我遇到了同样严重的问题,即使是在安装了相同Qt的不同PC上。当我在qrc文件中的每个前缀中添加最后一个斜杠时,效果很好:

<qresource prefix="/images/">


在那之后,我删除了所有缓存的.qrc并重新编译,它工作得很好。

谢谢你的建议,不,我已经尝试过了,它似乎不工作,那么你的
main.qrc
文件中有什么?我已经在做了,仍然没有更改(cf更新的帖子)谢谢你的建议,不,我已经试过了,它似乎不工作,那么你的
main.qrc
文件中有什么?我已经在做了,仍然没有改变(cf updated post)如果图像在你的资源文件中,它会内置到应用程序中。你根本不需要将图像单独存储在你的设备上。啊,我不知道如果图像在你的资源文件中,它就会内置到应用程序中。你根本不需要将图像单独存储在你的设备上。啊,我不知道thanksThanks,这确实与我的代码类似。但是我认为这个问题是由目标设备中的目录结构引起的。源代码是在二进制文件中编译的,因此它应该独立于目标文件夹系统。谢谢,这确实与我的代码类似。但是,我认为问题是由目标设备中的目录结构引起的。源代码是在二进制文件中编译的,因此它应该独立于目标文件夹系统。