Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

extracting a list from a QAbstractItemModel



  • 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


  • Lifetime Qt Champion

    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 ?



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



  • 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



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



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



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



  • 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



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


Log in to reply