使用Qt Quick XmlListModel读取本地XML文件

问题描述 投票:0回答:1

我有这样的XML文件(data.xml):

<?xml version="1.0" encoding="UTF-8"?>
<table name="atome">
    <column name="id">1</column>
    <column name="nom">Hydrogène</column>
    <column name="slug">hydrogene</column>
    <column name="electron">1</column>
    <column name="numero">1</column>
    <column name="symbole">H</column>
    <column name="info_groupe">1</column>
    <column name="info_periode">1</column>
    <column name="info_bloc">s</column>
</table>

我可以使用QML查看此XML文件的内容:

import QtQuick 2.7
import QtQuick.Controls 2.0
import QtQuick.Layouts 1.0
import QtQuick.XmlListModel 2.0

ApplicationWindow {
    visible: true
    width: 640
    height: 480

    XmlListModel {
        id: model
        source: "qrc:/data.xml"
        query: "/table/column"
        XmlRole { name: "columnName"; query: "string(@name)" }
        XmlRole { name: "columnValue"; query: "string()" }
    }

    ListView {
        anchors.fill: parent
        model: model
        delegate:  Text { text: columnName + " = " + columnValue }
    }

}

但是如果我将文件放到/var/tmp/data.xml并将源更改为源:“ / var / tmp / data.xml”,我什么也看不到。

源:“ /var/tmp/data.xml”

任何想法?

xml qt qtquick2
1个回答
0
投票
确定,应该是“源:“ file:///var/tmp/data.xml”“
© www.soinside.com 2019 - 2024. All rights reserved.