QML Image obtains the details of the Image resource path,

Source: Internet
Author: User

QML Image obtains the details of the Image resource path,

Recently, Qt5 is restarted. When QML is used to obtain local Image resources, it always encounters an error similar to "QML Image: Cannot open: qrc: // images/Blue hills.jpg, that is, the local image resources cannot be loaded normally. After a general effort, the pull is finally completed! We hereby record it for you and your colleagues to check it!

Reference: http://mobile.51cto.com/symbian-261878.htm

 

When I first encountered a problem, I searched the internet for a long time and failed to solve the problem. However, it was somewhat enlightening. For example, I had some experience in the information I saw on the above website.

Here, I will first post my small example:

Rectangle {
    id: rec
    width: 800
    height: 600
    opacity: 1
 
    Image {
        id: image1
        source: "images/Blue hills.jpg"
 
        Image {
            id: image2
            width: 256
            height: 256
            source: "images/cute_colorful_qq_01.png"
        }
    }
}
 
 

If it is displayed normally, the following results will be displayed:

However, when I started the test, the program running results were all blank and the error "QML Image: Cannot open: qrc: // images/Blue hills.jpg" always appeared, although the program can run normally!

 

At first, I thought it was a path problem. According to many online documents, ijiu continuously modified the path parameters! No matter how you modify it, you cannot display the image. Later, I suspected it was a Qt Bug. However, I tested how to load the remote image, that is, the picture on the network, such as the following code:

    width: 800
    height: 600
    opacity: 1
 
    Image {
        id: image1
        source: "http://192.168.13.219/images/Blue hills.jpg"
    }
}
 
 

The URL next to the source is the image under the path of the server on your computer. You can also link it to other image links on the network! In this way, it can be displayed normally, for example:

 

From this point of view, it is not a problem of Qt, that is, your own problem. Continue to explore!

Note the following:

 

Later, I wondered if the resource file "qml. qrc" had a problem. Open it and check that the content is as follows:

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

Well, it seems that there is a problem, because, as a general rule, *. qrc resource file records the path information of image resources, but it does not exist here, So I manually changed it to the following:

<RCC>    <qresource prefix="/">        <file>main.qml</file>        <file>images/Blue hills.jpg</file>        <file>images/cute_colorful_qq_01.png</file>        <file>images/Sunset.jpg</file>    </qresource></RCC>


Then re-compile and run the program. This is an exciting time and our image is successfully displayed! Haha!

 

Here we can remind you that you do not need to manually change the *. qrc file. You can simply load the image resources in the Project of QtCreator! As shown in:

 

Do you see the images circled in the red box above? You need to manually add them to the project so that the "*. qrc file, which is unavailable at the beginning! The problem is here!

 

Okay! Come here!




Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.