Can't load Image source in QML Image

∥☆過路亽.° 提交于 2020-01-01 04:07:46

问题


This seems really basic but for some reason I can't get the image source to work in the ultra-simple QML app below.

FYI, I'm running Mac OS-X 10.9.5, Qt Creator 3.2.1 based on Qt 5.3.2.

import QtQuick 2.3
import QtQuick.Controls 1.2

ApplicationWindow {
    visible: true
    width: 640
    height: 480
    title: qsTr("Test")

    Image {
        id: image1
        x: 10
        y: 10
        width: 100
        height: 100
        source: "testImage.png"
    }
} 

I get the following error:

qrc:/main.qml:10:5: QML Image: Cannot open: qrc:/testImage.png

The application window is created but no image is displayed. I've also tried wrapping the image within a Rectangle but this doesn't help.

What am I doing wrong here?

The "testImage.png" is in the project directory and I've tried all sorts of ways to specify the image path using resources, absolute, relative, and even specifying the image source manually with the QML UI designer.

I'll also mention that to get Qt 5.3.2 to work I followed the modification suggested HERE.

Thanks.


回答1:


I suppose that your qml file is in resources, so try place your testImage.png image in resources too. Recompile app, run it and check is image works properly.

All should be together. For example:

My code:

Image
{
    source: "images/earth.png"

If I delete this image from resources, I'll get this error too, but image still in the correct place in file system.

When qml source placed in the resource, then all files, which loads from qml should be in resource too (Qt searchs this files in the resources, not in the file system).




回答2:


If you are locating from local filesystem , get the file path and prefix "file:" as source = "file:/root/Desktop/Rathod.png"




回答3:


There can be two problems:

  1. The image is corrupt, although the image was correctly displayed in all previews IconImage refuses to load it. Try to use another image that is known as work correctly It seems that the Qt Image loader is very sensitive to the image format.
  2. There is a resource cache problem. Try to delete qml_qmlcachea.qrc in your compiling folder and try it again



回答4:


If you are using qrc, make sure the image is added to your project. Just having it in your source directory may not be enough. For example, my main.qml is located in a particular directory. I move my .png into the same directory. I then "Add Existing Files.." and add the .png to the project. My image has the following

source:"qrc:<same dir as main.qml>/image.png"


来源:https://stackoverflow.com/questions/26125095/cant-load-image-source-in-qml-image

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!