QML Image: Cannot open: qrc:/image/1.png

来源:互联网 发布:人工智能开发语言 编辑:程序博客网 时间:2024/05/21 11:10

QML获取本地图片资源时总是遇到类似 “ QML Image: Cannot open: qrc:///images/Blue hills.jpg ”的错误,即无法正常载入本地的图片资源,经过一般努力,终于搞定。。。


但是本人一开始测试的时候,程序运行的效果都是一片空白,而且还总是出现 “ QML Image: Cannot open: qrc:///images/Blue hills.jpg ” 的错误,虽然程序可以正常运行!


I think you should get rid of relative path to your image. The path should be like qrc:/images/LA.png.


source: "images/cute_colorful_qq_01.png"

    

1  把图片放到当前路径下,也就是和.pro一个路径;

2  点击有main.qml的那个qrc用editor打开,然后添加这个图片文件;

3  右击新添加的图片copy the url 。然后呢,source:"qrc:/001.png".出来了。


From : QML Image获取图片资源路径的细节


The URL may be absolute, or relative to the URL of the component. qrc URLs are relative. You can also use file:///home/user/testImage.png , or you can define your own QQuickImageProvider that can handle your own scheme, e.g. Image { source: "image://myimageprovider/testImage.png" } – bitek Apr 21 at 12:38 


From: qml中Image的source属性路径


只能从报错路径qrc/image/1.png另想办法。


因为代码中写的是相对路径,所以图片路径可能和qml文件的本身路径邮箱

后来发现在main.cpp中加载qml的路径是这样的:

QQmlApplicationEngine engine;

engine.load(QUrl(QStringLiteral("qrc:/main.qml")));
然后改为不从qrc路径加载后程序能正常加载图片了:    QQmlApplicationEngine engine;
//    engine.load(QUrl(QStringLiteral("qrc:/main.qml")));
    engine.load("main.qml"); 


问题已经解决一半,发布程序的时候肯定不能将qml暴露出来啊!

所以还是只能按照原来的方式加载qml。


后来查看qt文档 Image 中的 source属性为url,我写的相对路径的字符串被转化成了qrc:/image/1.png这样的url变量

还有没有其他形式的url变量呢?

想了很久,突然灵光乍现想到之前用FileDialog的时候,返回选中文件的路径是"file:///xxxx.png"

于是在现在的代码中将source的路径改为

Image{

        source: "file:///E:/seven_code/code/ImageSource/image/1.png"
    }
程序正常加载图片,问题解决一大半!



原创粉丝点击