简体   繁体   中英

Qt - Configuring interface through xml

I have a local xml file telling me:

  • 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. How can I do, considering that I am working in 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!

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.

One XmlListModel would provide the input for how many pages there are

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.

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".

Parsing done only once, eg with QXmlStreamReader to build the list of "row models" and getting the contents of each of them.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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