首页 > 解决方案 > QML XmlListModel 递归(?)查询

问题描述

我有一个看起来像这样的 xml:

<?xml version="1.0" encoding="UTF-8" ?>
<root>
    <plcValue>dgIconXY11.value</plcValue>
    <state>Grip1IsClamped.png</state>
    <state>Grip1IsMove.png</state>
</root>

我用这个查询这个xml:

import QtQuick 2.12
import QtQuick.XmlListModel 2.12

XmlListModel {
    id: xmlIconModel

    property int stateNr

    source: "listViewIcons.xml"
    query: "/root"

    XmlRole {
        name: "sPLCval"
        query: "plcValue/string()"
    }

    XmlRole {
        name: "sState"
        query: "state[" + stateNr + "]/string()"
    }
}

然后使用它:

import QtQuick 2.12

Rectangle {
    id: iconGridBg

    property int currIndex

    width: 36
    height: 36

    color: "black"

    Image {
        id: iconListImg
        height: 36
        width: 36

        source: sState
    }
}

问题:我想使用 plcValue 字符串并从代码中获取它的值,并且使用该值我想获取同一分支的第 n 个状态值。

所以我的dgIconXY11.value代码中有一个地方。它的值将是一个 int 并且基于该 int 如果它是例如: 1 我想获得第一个状态,如果它是 2 第二个状态。

标签: xmlxpathqml

解决方案


所以,我没有成功使用递归查询。解决方法是有两个(或更多)XmlListModels。首先获取第二个查询的信息,就像sPLCval我的情况一样。


推荐阅读