Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Store XML data in a String Array using ListView

Store XML data in a String Array using ListView

Scheduled Pinned Locked Moved Unsolved General and Desktop
7 Posts 2 Posters 2.5k 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.
  • B Offline
    B Offline
    Bongani
    wrote on 17 Jun 2016, 13:45 last edited by
    #1

    I have and xml string.

    
    <Output>
         <user>
         <name>Allan</name>
         <surname>Smith</surname>
         <age>64<age>
      </user>
      <user>
         <name>Bradley</name>
         <surname>Carrel</surname>
         <age>58<age>
      </user>
      <user>
         <name>Sam</name>
         <surname>Johnson</surname>
         <age>24<age>
      </user>
    </Output>
    

    I want to store the xml data inside three String arrays, eg names[], surnames[], ages[].

    I am using QML's list view to read the xml data :

    
        ListView{
            anchors.fill: parent
            id: schemelist
    
            model: XmlListModel{
                id: xmlListModel
                query: "/Output/user"
                XmlRole{
                    name : "name" ;
                    query: "name/string()";
                }
                XmlRole{
                    name : "surname" ;
                    query: "surname/string()";
                }
                XmlRole{
                    name : "age" ;
                    query: "age/string()";
                }
            }
    
            delegate:Item {
                     //I need to find the way to push the values into the arrays as the are processed
                    //eg
                    function storedata(){
                           names.push(name);
                           surnames.push(surname);
                           ages.push(ages)
                   }
                }
        }
    
    

    I need to store the data read from the xml to my arrays as it's processed from the ListView. Please help.

    1 Reply Last reply
    0
    • V Offline
      V Offline
      VRonin
      wrote on 17 Jun 2016, 14:32 last edited by
      #2

      Check XmlListModel

      "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
      ~Napoleon Bonaparte

      On a crusade to banish setIndexWidget() from the holy land of Qt

      1 Reply Last reply
      1
      • B Offline
        B Offline
        Bongani
        wrote on 17 Jun 2016, 15:14 last edited by
        #3

        @VRonin i did check that hance i used it, the problem is delegating the Xmllist model to a function that will store the string data as its being processed.

        1 Reply Last reply
        0
        • B Offline
          B Offline
          Bongani
          wrote on 17 Jun 2016, 15:16 last edited by
          #4

          eg.

          
              ListView{
                  anchors.fill: parent
                  id: schemelist
          
                  model: XmlListModel{
                      id: xmlListModel
                      query: "/GetQuote_pinnafrica_Output/Get_Quote_Response"
                      XmlRole{
                          name : "Scheme_Code" ;
                          query: "Scheme_Code/string()";
                      }
                      XmlRole{
                          name : "Scheme_Desciption" ;
                          query: "Scheme_Description/string()";
                      }
                      XmlRole{
                          name : "Gross_Premium" ;
                          query: "Gross_Premium/string()";
                      }
                      XmlRole{
                          name : "Premium_Type" ;
                          query: "Premium_Type/string()";
                      }
                      XmlRole{
                          name : "Quote_Number" ;
                          query: "Quote_Number/string()";
                      }
          
                  }
          
                  delegate:Item {
                             function storevalues(){
                                     names.push(names)
                                     surnames.push(surname)
                                     ages.push(age)
                           }
                      }
              }
          
          
          1 Reply Last reply
          0
          • V Offline
            V Offline
            VRonin
            wrote on 17 Jun 2016, 15:48 last edited by
            #5

            Personally I'd do this in C++ but if you want to stick to declarative check the add() signal

            "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
            ~Napoleon Bonaparte

            On a crusade to banish setIndexWidget() from the holy land of Qt

            1 Reply Last reply
            0
            • B Offline
              B Offline
              Bongani
              wrote on 20 Jun 2016, 12:34 last edited by Bongani
              #6

              Thanks for the reply @VRonin . I decide to take your advice on using c++ to do all the array work.

              i created a object class

              MainObject.h

              
              #ifndef MAINOBJECT_H
              #define MAINOBJECT_H
              
              #include <QObject>
              #include <QDebug>
              
              class MainObject : public QObject{
                 Q_OBJECT
              public:
                  explicit MainObject (QObject* parent = 0) : QObject(parent) {}
                  Q_INVOKABLE void addvalues(QString name,QString surname,QString age)
                  {
                      qDebug() << "name      : " << name;
                      qDebug() << "surname   : " << surname;
                      qDebug() << "age       : " << age;
                  }
              };
              
              #endif // MAINOBJECT_H
              
              

              Registered it in my main.cpp file

              qmlRegisterType<MainObject>("com.mycode", 1, 0, "MainObject");
              

              And called it in my .qml file

              
              import com.mycode 1.0
              .
              .
              .
              .
              
              MainObject.addvalues(name,surname,age);
              
              

              but now i am getting this error :

              qrc:/test.qml:69: TypeError: Property 'addvalues' of object [object Object] is not a function
              

              Is there something i am missing, Please help

              1 Reply Last reply
              0
              • V Offline
                V Offline
                VRonin
                wrote on 20 Jun 2016, 12:57 last edited by
                #7

                I actually meant implementing the model as QAbstractItemModel in C++ and use it instead of the XmlListModel.

                "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
                ~Napoleon Bonaparte

                On a crusade to banish setIndexWidget() from the holy land of Qt

                1 Reply Last reply
                0

                1/7

                17 Jun 2016, 13:45

                • Login

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