[PyQt] QTreeView: inserting and removing rows in view and
underlying data...
Dirk Wagener
dwagen at stonethree.com
Fri Nov 9 08:58:08 GMT 2007
Hi
I am experiencing some confusion regarding the model/view architecture
when using a QTreeView with a custom
QAbstractItemModel.
This is the bigger picture of what I am doing:
* I use a tree view to browse and edit a bunch of object.
* I have a data class (TreeNode) used to store my data. This is a
typical node class with a parent and children. Each TreeNode
represents an object that gets displayed in the tree view. A TreeNode
has members parent, childNodes (list of TreeNode children) and
a bunch more.
I am confused about the interaction between the view and the underlying
data. I read the Qt docs, but find them quite confusing regarding this
matter.
How do I protect and synchronise interaction with the underlying data?
When I populate a TreeNode, eg when I add children to a TreeNode, how do
I tell the view to wait and only update when I am done?
*Should I do this in a beginInsertRows() - endInsertRows() block?
# Get the model index for the parent TreeNode.
parentModelIdx = self.createIndex(parent.row(), 0, parent)
# childNodes is a list of child TreeNodes.
# insert a new child at end of list of children
self.beginInsertRows(parentModelIdx,len(parent.childNodes),len(parent.childNodes))
# Add the child TreeNode to our underlying data (parent).
parent.addChild( child )
self.endInsertRows()
Should I do this for every node I add?
Is it acceptable to create the parent model index every time?
* OR, should I do something like this:
# Tell the view data is going to change:
self.emit( QtCore.SIGNAL( "layoutAboutToBeChanged()" ) )
# Change the data by adding a child TreeNode:
parent.addChild( child )
# Tell the view we are done.
self.emit( QtCore.SIGNAL( "layoutChanged()" ) )
* OR, should I do both??
The same sort of questions apply to removing rows.
At this stage I am doing something wrong regarding this. Now and then
(sporadically) I get access violations when getting the
underlying data from a QModelIndex():
tmpTreeNode = currentModelIndex.internalPointer()
In this case I would expect tmpTreeNode to be of type TreeNode, but I
now and then get random data back, even though
currentModelIndex.isValid() is true.
I expect this has something to do with the way in which I populate my
tree view and underlying data. My model indexes are getting messy!
Can somebody please give me some advice. Where can I learn how to do
this model/view interaction properly?
Are there some rules which I am not obeying here??
Kind regards
Dirk Wagener
--
Dirk Wagener <dwagen at stonethree.com>
(MSc. Electronic Engineering, Stell.)
Project Engineer
Stone Three Signal Processing (Pty.) Ltd.
http://www.stonethree.com
+27 21 851 3123 (phone)
+27 21 851 3127 (fax)
We shall not cease from exploration and the end of all our exploring will
be to arrive where we started... and know the place for the first time.
T.S. Eliot
More information about the PyQt
mailing list