Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. XmlListModel empty data
QtWS25 Last Chance

XmlListModel empty data

Scheduled Pinned Locked Moved Solved QML and Qt Quick
qtquickxmllistmodellistview
3 Posts 2 Posters 1.7k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • bguivarchB Offline
    bguivarchB Offline
    bguivarch
    wrote on last edited by bguivarch
    #1

    Hello,

    I am trying to parse a local XML file using XmlListModel.
    The problem is, even if the status of my model doesn't return an error code, its count property always returns 0, and my ListView stays empty.
    I can't understand why it is not working, so I am asking for your help!

    Here is my code !

    Item {
        property int modelStatus:listViewModel.status
        id: main
        width: 480
        height: 762
        state:Screen.primaryOrientation === Qt.PortraitOrientation ? "" : "Landscape"
    
        Component.onCompleted: {
            console.log("xml="+listViewModel.xml);
            console.log("status="+listViewModel.status);
            console.log("count="+listViewModel.count);
            console.log("roles="+listViewModel.roles);
        }
    
        onModelStatusChanged:
        {
            console.log("model status is "+modelStatus);
            console.log("count="+listViewModel.count);
            console.log("roles="+listViewModel.roles);
        }
    
    XmlListModel
        {
            id:listViewModel
            source:"gamescollection.xml"
            query:"/games/game"
            XmlRole{name:"name";query:"name/string()"}
            XmlRole{name:"code";query:"code/string()"}
            XmlRole{name:"playersmin";query:"playersmin/number()"}
            XmlRole{name:"playersmax";query:"playersmax/number()"}
            XmlRole{name:"time";query:"time/number()"}
        }
    Component
        {
            id:listViewDelegate
            Item{
                height:64
                width:gamesListView.width
    
                Text {
                    id: rowLabel
                    text: name+code+playersmin+playersmax+time
                    verticalAlignment: Text.AlignVCenter
                    font.pixelSize: 24
                    anchors.fill: parent
                }
    
                MouseArea {
                    anchors.fill: parent
                    onClicked: {
                        gamesListView.currentIndex = index;
                    }
                }
            }
        }
    
        ListView {
            id: gamesListView
            width: parent.width - (anchors.leftMargin*2)
            height: 400
            contentHeight: 400
            anchors.top: header1.bottom
            anchors.topMargin: 32
            anchors.left: parent.left
            anchors.leftMargin: 32
            model: listViewModel
            delegate: listViewDelegate
            clip:true
            highlight: Rectangle{
                color:mainWindow.m_WidgetSelectedColor
                opacity: 0.25
            }
            highlightMoveDuration: -1
            highlightMoveVelocity: -1
            focus:true
            currentIndex: 0
        }
    }
    

    And this is my XML file:

    <?xml version=1.0" encoding="utf-8" ?>
    <games>
    	<game>
    		<name>Libertalia</name>
    		<code>LIB</code>
    		<playersmin>2</playersmin>
    		<playersmax>6</playersmax>
    		<time>60</time>
    	</game>
    	<game>
    		<name>Aventuriers du Rail</name>
    		<code>ADR</code>
    		<playersmin>2</playersmin>
    		<playersmax>5</playersmax>
    		<time>90</time>
    	</game>
    </games>
    

    Thanks for your feebacks!

    Baptiste

    raven-worxR 1 Reply Last reply
    0
    • bguivarchB bguivarch

      Hello,

      I am trying to parse a local XML file using XmlListModel.
      The problem is, even if the status of my model doesn't return an error code, its count property always returns 0, and my ListView stays empty.
      I can't understand why it is not working, so I am asking for your help!

      Here is my code !

      Item {
          property int modelStatus:listViewModel.status
          id: main
          width: 480
          height: 762
          state:Screen.primaryOrientation === Qt.PortraitOrientation ? "" : "Landscape"
      
          Component.onCompleted: {
              console.log("xml="+listViewModel.xml);
              console.log("status="+listViewModel.status);
              console.log("count="+listViewModel.count);
              console.log("roles="+listViewModel.roles);
          }
      
          onModelStatusChanged:
          {
              console.log("model status is "+modelStatus);
              console.log("count="+listViewModel.count);
              console.log("roles="+listViewModel.roles);
          }
      
      XmlListModel
          {
              id:listViewModel
              source:"gamescollection.xml"
              query:"/games/game"
              XmlRole{name:"name";query:"name/string()"}
              XmlRole{name:"code";query:"code/string()"}
              XmlRole{name:"playersmin";query:"playersmin/number()"}
              XmlRole{name:"playersmax";query:"playersmax/number()"}
              XmlRole{name:"time";query:"time/number()"}
          }
      Component
          {
              id:listViewDelegate
              Item{
                  height:64
                  width:gamesListView.width
      
                  Text {
                      id: rowLabel
                      text: name+code+playersmin+playersmax+time
                      verticalAlignment: Text.AlignVCenter
                      font.pixelSize: 24
                      anchors.fill: parent
                  }
      
                  MouseArea {
                      anchors.fill: parent
                      onClicked: {
                          gamesListView.currentIndex = index;
                      }
                  }
              }
          }
      
          ListView {
              id: gamesListView
              width: parent.width - (anchors.leftMargin*2)
              height: 400
              contentHeight: 400
              anchors.top: header1.bottom
              anchors.topMargin: 32
              anchors.left: parent.left
              anchors.leftMargin: 32
              model: listViewModel
              delegate: listViewDelegate
              clip:true
              highlight: Rectangle{
                  color:mainWindow.m_WidgetSelectedColor
                  opacity: 0.25
              }
              highlightMoveDuration: -1
              highlightMoveVelocity: -1
              focus:true
              currentIndex: 0
          }
      }
      

      And this is my XML file:

      <?xml version=1.0" encoding="utf-8" ?>
      <games>
      	<game>
      		<name>Libertalia</name>
      		<code>LIB</code>
      		<playersmin>2</playersmin>
      		<playersmax>6</playersmax>
      		<time>60</time>
      	</game>
      	<game>
      		<name>Aventuriers du Rail</name>
      		<code>ADR</code>
      		<playersmin>2</playersmin>
      		<playersmax>5</playersmax>
      		<time>90</time>
      	</game>
      </games>
      

      Thanks for your feebacks!

      Baptiste

      raven-worxR Offline
      raven-worxR Offline
      raven-worx
      Moderators
      wrote on last edited by
      #2

      @bguivarch
      are you sure the application finds the XML file?
      You simply set the filename. Means it searches from the current working directory.

      Try a absolute or qrc path. In case of a absolute path precede "file:///" prefix to the url.

      --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
      If you have a question please use the forum so others can benefit from the solution in the future

      1 Reply Last reply
      0
      • bguivarchB Offline
        bguivarchB Offline
        bguivarch
        wrote on last edited by
        #3

        Hello,

        From the status value (which was either 1 or 2), I supposed that it had no problem findind the file.
        I solved the issue by myself, the header of my XML file was missing a double quote in the version value.
        Now it is working.

        Regards,

        1 Reply Last reply
        0

        • Login

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved