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. extracting a list from a QAbstractItemModel
Forum Updated to NodeBB v4.3 + New Features

extracting a list from a QAbstractItemModel

Scheduled Pinned Locked Moved Unsolved General and Desktop
9 Posts 3 Posters 1.1k Views 1 Watching
  • 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
    dammilo
    wrote on last edited by
    #1

    Hi guys,
    i'm kind of new to Python, Qt and Pyside and I'm trying to wrap my head around the MVC implementation.

    Problem is, I need to extract a list of leaves from a tree:

    0_1543252632151_Capture.PNG

    As you can see on the left i have a QTreeView with some SHOT nodes and some PASS nodes. On the right i have an empty QListView. On that list, I would like to output a list of the different PASS nodes, without SHOTS.
    ideally, if i can dream, that list should be a list without duplicates (but we should probably tackle a problem at a time)
    Everything has been implemented with a QAbstractItemModel. From what i understood, i need to use a Proxy Model (i have implemented one with the QSortFilterProxyModel, since i will need filtering, too: it looks like it's working).

    Right now i am trying to implement the mapToSource and mapFromSource functions, but i'm having a hard time at wrapping my head around them.
    Any insight, guide, example that can help me with this?
    Am i doing something wrong, in general?

    Fede

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi and welcome to devnet,

      Do you mean you want to see all of the Pass nodes or only a subset belonging to a shot ?

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      0
      • D Offline
        D Offline
        dammilo
        wrote on last edited by
        #3

        Hi sgaist, thanks!
        I'd like too see all of them. Possibly with no duplicates, if the name is the same. But this is step two, I'm just trying to understand the structure of qt's Mvc, at the moment.

        1 Reply Last reply
        0
        • VRoninV Offline
          VRoninV Offline
          VRonin
          wrote on last edited by
          #4

          While it's in my todo list. You can use this in the menatime: https://forum.qt.io/topic/71553/smarter-way-to-get-a-list-of-children-in-tree-model

          if the model is not too big it works seamlessly

          "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
          2
          • D Offline
            D Offline
            dammilo
            wrote on last edited by dammilo
            #5

            Hi Vronin, thanks for your help, i will look into it.
            In the meantime I experimented with the QProxyModel a little bit more....

            If i understand correctly: to have different views of the same underlying model I need to implement the methods mapToSource and mapFromSource to operate a mapping between different QModelIndex. Right?

            I read the doc, read forums, watched tutorials but I really cannot get a grasp of it. It's really frustrating because the general idea seems really straightforward, but during the coding phase i'm getting only inexplicable, buggy, uncontrollable results .

            Sadly after 2 days on this topic I'm starting to think that my knowledge of Qt is too limited and my schedule is too pressing to actually have enough time to look into this...

            1 Reply Last reply
            0
            • VRoninV Offline
              VRoninV Offline
              VRonin
              wrote on last edited by
              #6

              @dammilo said in extracting a list from a QAbstractItemModel:

              I read the doc, read forums, watched tutorials but I really cannot get a grasp of it. It's really frustrating because the general idea seems really straightforward, but during the coding phase i'm getting only inexplicable, buggy, uncontrollable results .

              Welcome to my life.

              What I found helps a lot is the Model Test and, if all else fail, bring out the big guns and debug using Gammaray

              "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
              • D Offline
                D Offline
                dammilo
                wrote on last edited by dammilo
                #7

                Ha, thanks. Those look like really big guns
                Just as a last test I went almost barebone, changing the tree model and the goal. This time i'd like to print a simple list of the shots available, and i only added 4 of them. Simple x to x relationship.
                As you will notice (spoiler) i can only get the first item to print:

                0_1543338091954_Capture.PNG

                this is what i tried:

                 def mapToSource(self, proxyIndex):
                
                        if(proxyIndex.isValid()):
                            row = proxyIndex.row()                                      # row of the index 
                            shots = self.sourceModel()._rootNode.child(0)               # this is the "SHOTS" node
                            node = shots.child(row)                                     # retrieve its "row-th" children 
                            return self.sourceModel().createIndex(row, 0, node);        # ask the sourceModel for a proper index
                
                        else:
                            return QModelIndex()
                
                    def mapFromSource(self, sourceIndex):
                
                        if(sourceIndex.isValid()):
                            node = self.sourceModel().getNode(sourceIndex)              # get the node  
                            row = node.row()                                            # ask for the index of the node inside the tree
                            return self.createIndex(row, 0, node);                      # return a proper index
                
                        else:
                            return QModelIndex()
                
                

                Again, at this point this is just for the sake of knowledge. What am i doing wrong?
                I'm pretty sure that the basic QAbstractItemModel implementation is right, since the QTreeView works without any problem....

                1 Reply Last reply
                0
                • VRoninV Offline
                  VRoninV Offline
                  VRonin
                  wrote on last edited by
                  #8

                  Since all shots have a common parent it's very easy to do. No need to use a proxy at all. Just set the entire model in the listview and use setRootIndex to The parent of all the shots

                  "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
                  • D Offline
                    D Offline
                    dammilo
                    wrote on last edited by dammilo
                    #9

                    I know this scenario is very easy and there are dozens of quicker ways to do this :) This is an artificial barebone testing ground, but I'd like to use the proper approach even if using it for such a small task is overkill. Again, just for the sake of understanding :)

                    The real world application will have a tree way more complex than this one and the one in the first post.

                    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