Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Qt for Python
  4. How to get all data from QStandardItem ?
QtWS25 Last Chance

How to get all data from QStandardItem ?

Scheduled Pinned Locked Moved Solved Qt for Python
qstandarditem
13 Posts 3 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.
  • D Offline
    D Offline
    Dariusz
    wrote on last edited by Dariusz
    #1

    Hey
    How can I get all roles/data for given item in QStandardItem ? I want to serialize it all.

    Regards
    Dariusz

    Fixed, example code >

    i = QStandardItem("hello")
    i.setData("dasdas", 15241)
    i.setData(5325, 15242)
    i.setData(["vdsvds"], 15243)
    i.setData(["vdsvds"], 15245)
    
    ar = QByteArray()
    s = QDataStream(ar, QIODevice.WriteOnly)
    i.write(s)
    r = QDataStream(ar, QIODevice.ReadOnly)
    val = 0
    v = r.readUInt32()
    for a in range(v):
        role = r.readInt32()
        var = r.readQVariant()
        print(role, var)
    
    JonBJ 1 Reply Last reply
    0
    • D Dariusz

      Mmm I was hoping to save it to json or something, there is no way to get list of roles to loop over manually?

      JonBJ Offline
      JonBJ Offline
      JonB
      wrote on last edited by JonB
      #8

      @Dariusz
      It is hidden from you in private code. A vector of <role-number, value> is how it is stored and serialized. The only way you can inspect this --- and thereby get the role names used for each item --- is by doing the serialization to QDataStream per my above and then using your own structures/code to read it back, so that you can access the elements and get the roles. See Qt source for this, e.g. https://codebrowser.dev/qt5/qtbase/src/gui/itemmodels/qstandarditemmodel_p.h.html#_ZlsR11QDataStreamRK17QStandardItemData

      D 1 Reply Last reply
      0
      • D Dariusz

        Hey
        How can I get all roles/data for given item in QStandardItem ? I want to serialize it all.

        Regards
        Dariusz

        Fixed, example code >

        i = QStandardItem("hello")
        i.setData("dasdas", 15241)
        i.setData(5325, 15242)
        i.setData(["vdsvds"], 15243)
        i.setData(["vdsvds"], 15245)
        
        ar = QByteArray()
        s = QDataStream(ar, QIODevice.WriteOnly)
        i.write(s)
        r = QDataStream(ar, QIODevice.ReadOnly)
        val = 0
        v = r.readUInt32()
        for a in range(v):
            role = r.readInt32()
            var = r.readQVariant()
            print(role, var)
        
        JonBJ Offline
        JonBJ Offline
        JonB
        wrote on last edited by
        #2

        @Dariusz
        void QStandardItem::write(QDataStream &out) const

        Writes the item to stream out. Only the data and flags of the item are written, not the child items.

        1 Reply Last reply
        2
        • D Offline
          D Offline
          Dariusz
          wrote on last edited by
          #3

          Mmm I was hoping to save it to json or something, there is no way to get list of roles to loop over manually?

          Christian EhrlicherC JonBJ 2 Replies Last reply
          0
          • D Dariusz

            Mmm I was hoping to save it to json or something, there is no way to get list of roles to loop over manually?

            Christian EhrlicherC Online
            Christian EhrlicherC Online
            Christian Ehrlicher
            Lifetime Qt Champion
            wrote on last edited by
            #4

            @Dariusz said in How to get all data from QStandardItem ?:

            there is no way to get list of roles to loop over manually?

            You know which roles you're setting so you can also iterate over them.

            Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
            Visit the Qt Academy at https://academy.qt.io/catalog

            D 1 Reply Last reply
            0
            • Christian EhrlicherC Christian Ehrlicher

              @Dariusz said in How to get all data from QStandardItem ?:

              there is no way to get list of roles to loop over manually?

              You know which roles you're setting so you can also iterate over them.

              D Offline
              D Offline
              Dariusz
              wrote on last edited by
              #5

              @Christian-Ehrlicher Well, partially. User might set custom roles which I wont be able to track. So I'd like to query roles from item...

              Christian EhrlicherC 1 Reply Last reply
              0
              • D Dariusz

                @Christian-Ehrlicher Well, partially. User might set custom roles which I wont be able to track. So I'd like to query roles from item...

                Christian EhrlicherC Online
                Christian EhrlicherC Online
                Christian Ehrlicher
                Lifetime Qt Champion
                wrote on last edited by
                #6

                @Dariusz said in How to get all data from QStandardItem ?:

                User might set custom roles

                I thought you're doing the programming, not the user...

                Anyway - derive from QStandardItemModel and remember the roles which are getting set.

                Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                Visit the Qt Academy at https://academy.qt.io/catalog

                D 1 Reply Last reply
                0
                • Christian EhrlicherC Christian Ehrlicher

                  @Dariusz said in How to get all data from QStandardItem ?:

                  User might set custom roles

                  I thought you're doing the programming, not the user...

                  Anyway - derive from QStandardItemModel and remember the roles which are getting set.

                  D Offline
                  D Offline
                  Dariusz
                  wrote on last edited by Dariusz
                  #7

                  @Christian-Ehrlicher Ok to summarize, there is no "getRoles" way of getting roles from QStandardItem and I have to implement it :- )

                  Will work on that, thanks!

                  1 Reply Last reply
                  0
                  • D Dariusz

                    Mmm I was hoping to save it to json or something, there is no way to get list of roles to loop over manually?

                    JonBJ Offline
                    JonBJ Offline
                    JonB
                    wrote on last edited by JonB
                    #8

                    @Dariusz
                    It is hidden from you in private code. A vector of <role-number, value> is how it is stored and serialized. The only way you can inspect this --- and thereby get the role names used for each item --- is by doing the serialization to QDataStream per my above and then using your own structures/code to read it back, so that you can access the elements and get the roles. See Qt source for this, e.g. https://codebrowser.dev/qt5/qtbase/src/gui/itemmodels/qstandarditemmodel_p.h.html#_ZlsR11QDataStreamRK17QStandardItemData

                    D 1 Reply Last reply
                    0
                    • JonBJ JonB

                      @Dariusz
                      It is hidden from you in private code. A vector of <role-number, value> is how it is stored and serialized. The only way you can inspect this --- and thereby get the role names used for each item --- is by doing the serialization to QDataStream per my above and then using your own structures/code to read it back, so that you can access the elements and get the roles. See Qt source for this, e.g. https://codebrowser.dev/qt5/qtbase/src/gui/itemmodels/qstandarditemmodel_p.h.html#_ZlsR11QDataStreamRK17QStandardItemData

                      D Offline
                      D Offline
                      Dariusz
                      wrote on last edited by
                      #9

                      Hey @JonB

                      Mmm tried, I don't think its working in pyside2

                      i = QStandardItem("hello")
                      i.setData("dasdas", 15241)
                      i.setData(5325, 15242)
                      i.setData(["vdsvds"], 15243)
                      
                      ar = QByteArray()
                      s = QDataStream(ar)
                      i.write(s)
                      print(ar)
                      

                      Got an empty buffer.
                      Regards
                      Dariusz

                      JonBJ 1 Reply Last reply
                      0
                      • D Dariusz

                        Hey @JonB

                        Mmm tried, I don't think its working in pyside2

                        i = QStandardItem("hello")
                        i.setData("dasdas", 15241)
                        i.setData(5325, 15242)
                        i.setData(["vdsvds"], 15243)
                        
                        ar = QByteArray()
                        s = QDataStream(ar)
                        i.write(s)
                        print(ar)
                        

                        Got an empty buffer.
                        Regards
                        Dariusz

                        JonBJ Offline
                        JonBJ Offline
                        JonB
                        wrote on last edited by JonB
                        #10

                        @Dariusz
                        I don't know, but I would flush & close the data stream before I relied on whatever might be in the array.

                        I also don't know for sure how this works when you have no model.

                        D 1 Reply Last reply
                        0
                        • JonBJ JonB

                          @Dariusz
                          I don't know, but I would flush & close the data stream before I relied on whatever might be in the array.

                          I also don't know for sure how this works when you have no model.

                          D Offline
                          D Offline
                          Dariusz
                          wrote on last edited by
                          #11

                          Hey @JonB yep I messed up, forgot to specify stream mode

                          i = QStandardItem("hello")
                          i.setData("dasdas", 15241)
                          i.setData(5325, 15242)
                          i.setData(["vdsvds"], 15243)
                          i.setData(["vdsvds"], 15245)
                          
                          ar = QByteArray()
                          s = QDataStream(ar, QIODevice.ReadWrite)
                          i.write(s)
                          r = QDataStream(ar, QIODevice.ReadOnly)
                          val = 0
                          v = r.readInt32()
                          for a in range(v):
                              print(r.readInt32())
                          print(v)
                          

                          I've digged in to the streaming. Looks to be a bit more of a pain to de-stream it tbh. Tricky.
                          Its not a list of roles
                          its role, data, role, data, role, data etc etc

                          JonBJ 1 Reply Last reply
                          0
                          • D Dariusz

                            Hey @JonB yep I messed up, forgot to specify stream mode

                            i = QStandardItem("hello")
                            i.setData("dasdas", 15241)
                            i.setData(5325, 15242)
                            i.setData(["vdsvds"], 15243)
                            i.setData(["vdsvds"], 15245)
                            
                            ar = QByteArray()
                            s = QDataStream(ar, QIODevice.ReadWrite)
                            i.write(s)
                            r = QDataStream(ar, QIODevice.ReadOnly)
                            val = 0
                            v = r.readInt32()
                            for a in range(v):
                                print(r.readInt32())
                            print(v)
                            

                            I've digged in to the streaming. Looks to be a bit more of a pain to de-stream it tbh. Tricky.
                            Its not a list of roles
                            its role, data, role, data, role, data etc etc

                            JonBJ Offline
                            JonBJ Offline
                            JonB
                            wrote on last edited by
                            #12

                            @Dariusz
                            But I said earlier

                            A vector of <role-number, value> is how it is stored and serialized.

                            Is it not exactly that? And you can deserialize into a QVector.

                            D 1 Reply Last reply
                            0
                            • JonBJ JonB

                              @Dariusz
                              But I said earlier

                              A vector of <role-number, value> is how it is stored and serialized.

                              Is it not exactly that? And you can deserialize into a QVector.

                              D Offline
                              D Offline
                              Dariusz
                              wrote on last edited by Dariusz
                              #13

                              @JonB I'm in python, I cant just do stream >> QVector<QStandardItemData> I'm afraid :/
                              I'd have to deserialize each data object by hand to find correct offset byte to next role in vector I think

                              Update!
                              Welp turns out there is readQVariant!

                              i = QStandardItem("hello")
                              i.setData("dasdas", 15241)
                              i.setData(5325, 15242)
                              i.setData(["vdsvds"], 15243)
                              i.setData(["vdsvds"], 15245)
                              
                              ar = QByteArray()
                              s = QDataStream(ar, QIODevice.ReadWrite)
                              i.write(s)
                              r = QDataStream(ar, QIODevice.ReadOnly)
                              val = 0
                              v = r.readUInt32()
                              for a in range(v):
                                  role = r.readInt32()
                                  var = r.readQVariant()
                                  print(role, var)
                              print(v)
                              

                              This works, wop wop!

                              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