使用qml时,载入图片文件,有几种方式。总结如下:
A.资源文件(会导致程序过大,甚至内存错误)
方法0:
Image{
source: "qrc:/images/backa.png"
anchors.fill: parent
}
B.文件路径
方法1:(绝对路径)
Image{
source:file:///C:/images/backa.png
anchors.fill: parent
}
方法2:(相对路径)
Image{
source: "file:images/backa.png"
anchors.fill: parent
}
方法3:(引入绝对路径)
main.cpp:
QQmlApplicationEngine engine;engine.rootContext()->setContextProperty("applicationDirPath", QGuiApplication::applicationDirPath());engine.load(QUrl(QStringLiteral("qrc:/main.qml")));
main.qml:
Image {source: "file:///"+applicationDirPath + "/images/backa.png"anchors.fill: parent}
以上方法经过亲自测试可用(qt 5.7.0)。部分网上有些方法测试无效。