无法在QML图像中加载图像源 [英] Can't load Image source in QML Image

查看:1660
本文介绍了无法在QML图像中加载图像源的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这看起来非常基本,但出于某种原因,我无法让图像源在下面的超简单QML应用程序中运行。

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

仅供参考,我正在运行基于Qt 5.3.2的Mac OS-X 10.9.5,Qt Creator 3.2.1。

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"
    }
} 

我收到以下错误:

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

创建应用程序窗口但不显示图像。我也尝试在Rectangle中包装图像,但这没有帮助。

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.

我在这里做错了什么?

testImage.png位于项目目录中,我尝试了各种方法来使用资源指定图像路径,绝对,相对,甚至使用QML UI设计器手动指定图像源。

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.

我还要提到为了让Qt 5.3.2正常工作,我按照修改建议 HERE

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

谢谢。

推荐答案

我认为您的 qml 文件位于资源中,因此请尝试放置 testImage.png 资源中的图像也是如此。重新编译应用程序,运行它并检查图像是否正常工作。

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:

我的代码:

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.

qml 源放在资源中时,然后从 qml 加载的所有文件也应该在资源中(Qt在资源中搜索这些文件,而不是在文件系统中搜索)。

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).

这篇关于无法在QML图像中加载图像源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆