QML无法多次打开图像

时间:2016-01-05 19:55:09

标签: image qt user-interface qml

在我的一个QML页面中,有1个后退按钮和一个包含图像和每行文本的列表视图。我对后退按钮和列表视图行项使用测试图像。后退按钮可以显示图像,但列表视图抱怨“QML图像:无法打开:qrc:/../../../ Users / Jerry / Desktop / LA.png”。

图像和所有QML文件都在Resources下。这是代码。

import QtQuick 2.0
import QtQuick.Controls 1.2

Rectangle {
anchors.fill: parent

Item {
    id: advancedBackground
    anchors.fill: parent

    Button {
        id: backButton
        width: 25
        height: 25
        anchors.top: parent.top
        anchors.topMargin: 25
        anchors.left: parent.left
        anchors.leftMargin: 25
        opacity: backMouseArea.containsMouse ? 0.9 : 0.1
        MouseArea {
            id: backMouseArea
            anchors.fill: parent
            hoverEnabled: true
            onClicked: {
                leftArrow.visible = true;
                rightArrow.visible = false;
                stackView.pop();
            }
        }
        Image {
            anchors.fill: parent
            fillMode: Image.PreserveAspectFit
            // image works fine here
            source: "qrc:/../../../Users/Jerry/Desktop/LA.png"
        }
    }

    ListView{
        id: listView
        anchors.rightMargin: 40
        anchors.leftMargin: 40
        anchors.bottomMargin: 30
        anchors.topMargin: 90
        anchors.fill: parent

        delegate: Item {
            x: 5
            width: parent.height
            height: 40

            Image {
                height: parent.height; width: parent.height
                anchors.left: parent.left
                fillMode: Image.PreserveAspectFit
                source: imageSource
            }

            Text {
                text: name
                height: parent.height
                width: parent.width
                anchors.left: parent.left
                anchors.leftMargin: 50
                anchors.centerIn: parent
                font.bold: true
                anchors.horizontalCenter: parent.horizontalCenter
            }
        }
        model: ListModel {
            ListElement {
                name: "test1"
                // image doesn't work here
                imageSource: "qrc:/../../../Users/Jerry/Desktop/LA.png"
            }

            ListElement {
                name: "test2"
                // image doesn't work here
                imageSource: "qrc:/../../../Users/Jerry/Desktop/LA.png"
            }
        }
    }
}

}

在资源下,它包括AdvancedPages.qml和../../../ Users / Jerry / Desktop / LA.png

1 个答案:

答案 0 :(得分:2)

我认为你应该摆脱你的形象的相对路径。路径应该像qrc:/images/LA.png