简体   繁体   English

Qt-通过xml配置接口

[英]Qt - Configuring interface through xml

I have a local xml file telling me: 我有一个本地xml文件告诉我:

  • how many "swiping" pages my interface must have 我的界面必须有多少个“滑动”页面
  • for any page, how many rows 对于任何页面,有多少行
  • for any row, how many columns (any column will contain a label and a value which depend on the attributes that you find between the nodes ) 对于任何行,有多少列(任何列将包含标签和值,这取决于您在节点之间找到的属性)

This is a template: 这是一个模板:

 <?xml version="1.0" encoding="utf-8"?>
 <parameters version="2.0">
      <page pageNum = "2">
           <row colNum = "1">
                <column>
                     <id>00001</id>
                     <name>parameterName</name>
                     <type>parameterType</type>
                     <value>parameterValue</value>
                     <min>0</min>
                     <max>100</max>
                     <step>1</step>
                </column>
           </row>
           <row colNum = "2">
                <column>
                     <id>00002</id>
                     <name>parameterName</name>
                     <type>parameterType</type>
                     <value>parameterValue</value>
                     <min>0</min>
                     <max>100</max>
                     <step>1</step>
                </column>
                <column>
                     <id>00003</id>
                     <name>parameterName</name>
                     <type>parameterType</type>
                     <value>parameterValue</value>
                     <min>0</min>
                     <max>100</max>
                     <step>1</step>
                </column>
           </row>
      </page>
      <page>
           <!-- ... etc ... -->
      </page>
 </parameters>

After some research I've found in some forums that XmlListModel is not suitable. 经过研究,我在某些论坛中发现XmlListModel不适合。 How can I do, considering that I am working in Qt? 考虑到我在Qt工作,该怎么办?

Here https://lists.launchpad.net/ubuntu-phone/msg12284.html I found something interesting, however for reading the xml file is used XMLHttpRequest, which I can't use, as my xml file is just local! 在这里https://lists.launchpad.net/ubuntu-phone/msg12284.html我发现了一些有趣的东西,但是在读取xml文件时使用的是XMLHttpRequest,我不能使用,因为我的xml文件只是本地的!

Can someone help me? 有人能帮我吗?

Thank you so much! 非常感谢!

While this can almost certainly be done with XmlListModel (see comment on the question) this would be fairly wasteful as each instance of the model needs to do all the parsing and querying. 尽管几乎可以肯定地使用XmlListModel完成此操作(请参阅问题注释),但这将非常浪费,因为模型的每个实例都需要执行所有的解析和查询。

One XmlListModel would provide the input for how many pages there are 一个XmlListModel将提供多少个页面的输入

XmlListModel {
    query: "/parameters/page"
}

Each page would then have a model that extracts the rows for that page (query not tested) 然后,每个页面都会有一个模型来提取该页面的行(未测试查询)

XmlListModel {
    // pageNum is a property set on each page, e.g. via the model.index of the Repeater generating the pages
    query: "/parameters/page[@pageNum=" + pageNum + "]/row"

    XmlRole { name: "id"; query: "column/id/string()" }
    // ... and so on
}

However, I think the best way forward is to create a custom QAbstractListModel subclass that can represent the rows of a single page. 但是,我认为最好的方法是创建一个自定义的QAbstractListModel子类,该子类可以表示单个页面的行。

Then an object with a list property of that type can be used as the model for the pages, each page using its "row model". 然后,具有该类型的list属性的对象可以用作页面的模型,每个页面都使用其“行模型”。

Parsing done only once, eg with QXmlStreamReader to build the list of "row models" and getting the contents of each of them. 解析仅完成一次,例如使用QXmlStreamReader来构建“行模型”列表并获取每个模型的内容。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM