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