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 11 Dec 2022, 11:16 last edited by Dariusz 12 Nov 2022, 14:40
    #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)
    
    J 1 Reply Last reply 11 Dec 2022, 11:34
    0
    • D Dariusz
      11 Dec 2022, 11:52

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

      J Online
      J Online
      JonB
      wrote on 11 Dec 2022, 12:19 last edited by JonB 12 Nov 2022, 12:20
      #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 11 Dec 2022, 14:24
      0
      • D Dariusz
        11 Dec 2022, 11:16

        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)
        
        J Online
        J Online
        JonB
        wrote on 11 Dec 2022, 11:34 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 11 Dec 2022, 11:52 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?

          C J 2 Replies Last reply 11 Dec 2022, 12:00
          0
          • D Dariusz
            11 Dec 2022, 11:52

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

            C Offline
            C Offline
            Christian Ehrlicher
            Lifetime Qt Champion
            wrote on 11 Dec 2022, 12:00 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 11 Dec 2022, 12:05
            0
            • C Christian Ehrlicher
              11 Dec 2022, 12:00

              @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 11 Dec 2022, 12:05 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...

              C 1 Reply Last reply 11 Dec 2022, 12:07
              0
              • D Dariusz
                11 Dec 2022, 12:05

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

                C Offline
                C Offline
                Christian Ehrlicher
                Lifetime Qt Champion
                wrote on 11 Dec 2022, 12:07 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 11 Dec 2022, 12:16
                0
                • C Christian Ehrlicher
                  11 Dec 2022, 12:07

                  @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 11 Dec 2022, 12:16 last edited by Dariusz 12 Nov 2022, 12:17
                  #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
                    11 Dec 2022, 11:52

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

                    J Online
                    J Online
                    JonB
                    wrote on 11 Dec 2022, 12:19 last edited by JonB 12 Nov 2022, 12:20
                    #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 11 Dec 2022, 14:24
                    0
                    • J JonB
                      11 Dec 2022, 12:19

                      @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 11 Dec 2022, 14:24 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

                      J 1 Reply Last reply 11 Dec 2022, 14:30
                      0
                      • D Dariusz
                        11 Dec 2022, 14:24

                        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

                        J Online
                        J Online
                        JonB
                        wrote on 11 Dec 2022, 14:30 last edited by JonB 12 Nov 2022, 14:31
                        #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 11 Dec 2022, 14:32
                        0
                        • J JonB
                          11 Dec 2022, 14:30

                          @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 11 Dec 2022, 14:32 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

                          J 1 Reply Last reply 11 Dec 2022, 14:33
                          0
                          • D Dariusz
                            11 Dec 2022, 14:32

                            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

                            J Online
                            J Online
                            JonB
                            wrote on 11 Dec 2022, 14:33 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 11 Dec 2022, 14:35
                            0
                            • J JonB
                              11 Dec 2022, 14:33

                              @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 11 Dec 2022, 14:35 last edited by Dariusz 12 Nov 2022, 14:38
                              #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

                              8/13

                              11 Dec 2022, 12:19

                              • Login

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