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

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

问题描述

这看起来很基本,但由于某种原因,我无法让图像源在下面的超简单 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.

仅供参考,我正在运行 Mac OS-X 10.9.5,基于 Qt 5.3.2 的 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 工作,我遵循了建议的修改 这里.

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 Image 中加载图像源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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