我想在一个简单的Qt项目中使用QtQuick Image对象,并将图像包含在项目中。我已将图像添加到资源文件中。我已使用调试来确保资源存在并作为“Resources/myfile.png”编译到应用程序中
但是,如果我声明 QtQuick 图像对象并将源设置为“Resource/myfile.png”,则找不到图像。相反,我收到一条消息:
QML Image: Cannot open: file:///C:/Users/me/Documents/QtCreator/build-myapp-Desktop_Qt_5_1_1_MinGW_32bit-Debug/qml/myapp/Resources/dial_bg.png
如果我尝试使用 C++ 方法访问文件,也会发生同样的情况
source: ":/Resources/dial_bg.png"
这会在编译时出现错误。
myfile.png 尚未复制到该位置。 myfile.png 没有出现在项目文件中,尽管它已添加到资源文件中。
我会很高兴有一个解决方案,要么将图像复制到图像想要拾取它的位置,要么让图像访问“编译”版本。
QML中访问此类资源的方式如下:
source: "qrc:///Resources/dial_bg.png"
qml.qrc 文件
<!DOCTYPE RCC><RCC version="1.0">
<qresource prefix="/">
<file alias="main.qml">qml/main.qml</file>
</qresource>
<qresource prefix="/pics/">
<file alias="bottom_arrow.png">pics/bottom_arrow.png</file>
</qresource>
</RCC>
在 main.cpp 文件中加载 QML 文件:
QString qml = QStringLiteral("qrc:///main.qml");
engine.load(QUrl(qml));
在 main.qml 文件中加载图像:
Image {
source: "pics/ef-logo.png"
}
在 Qt 中,它以某种方式 AUTORCC qrc 文件进行编码。
但是如果您使用 CMake 和 Visual Studio,那么在 CMake 中您需要包含以下内容:
set(CMAKE_AUTOMOC ON) #must have for QML to work
set(CMAKE_AUTORCC ON) #for compiling qrc file to rcc
set(AUTORCC_OPTIONS "src/qml.qrc")