PDA

View Full Version : How to view just a part of a model ?



weepdoo
6th June 2007, 17:19
Hye,

I'm actually developping an application in PyQt, with Qt4.1 and python 2.4
I have done my own QAbstractItemModel class (here 'M'), for managing my data (a tree like structure).
I've got already a QTreeView (here called 'S') connected to this model and it work great.

But, I would like that when a user click an element (or an index if you prefer here call 'I')of my tree:

display a new window (here is 'C')in my workspace (easy)
this window have a widget whitch is a treeview like item (just a treeview would be the best (I'm a bit lazy :P))
the model for the view should be 'M' (such as for 'S') BUT i do not want to display the parent (and siblings) of 'I', just his children


I try to make a 'picture':



The Tree in 'S':
[A]
|-[H]
|-[I]
| |-[K]
| |-[L]
...

The quiet same tree in 'C':
[I]
|-[K]
|-[L]
...

Does anyone know how can I do this ?

Michiel
6th June 2007, 17:41
Check out the QSortFilterProxyModel class. I think that might be what you're looking for.

You subclass it and install an instance of it between your model and treeview. Whenever you need to, you simply change the state in your filter-object, so it will only pass on the subtree you want to see.

wysota
6th June 2007, 18:01
Set the rootIndex of the view to the index of the item whose children are to be visible.

Michiel
6th June 2007, 18:20
Well. :) Turns out it was even easier than I thought. Sorry for the misdirection.

weepdoo
6th June 2007, 18:43
Easyer than I though :)
I will test it tomorow.

Thanks to all of you.
Thanks to TrollTech for this tremendous framework ^^

weepdoo
8th June 2007, 10:11
Hye everybody !

I still have a problem with my proxy.
I try to change my data root (as suggested earlier) so I redefine a new index method (to use my new data root) in my proxy as this (I repeat: it's python):


class QtgProxyModel(QtGui.QSortFilterProxyModel):
def __init__(self, model, newIndexRoot=None):
super(QtGui.QSortFilterProxyModel, self).__init__()

#======= Attributes Declaration =========
# We init the root of the proxy
self._data = model.getNodeFromIndex(newIndexRoot) # the data root
self._dObject = self._data.dDocObject # the dictonary of all children item
#========================================

self.setSourceModel(model)

def index(self, row, column, parent):
if not parent.isValid():
parentNode = self._data
else:
parentNode = self._dObject[parent.internalId()]

childItem = self._dObject[id(parentNode.getChildFromNum(row))]
if childItem is not None:
return self.createIndex(row, column, id(childItem))
else:
return QtCore.QModelIndex()



But, when launch my application, it just stop. I'm running on window, and DrWatson give an unreadable report.

After debugging, the only thing I know, is that the application blow away at line 21 at the "createIndex" call.

Do I do the right thing ? If not, how should I do it (I read C++ too :p )?
Is it a PyQt bug ?

Thanks

weepdoo
11th June 2007, 07:30
Nobody know what I'm doing wrong ?

Should I re implement the "index" method ?

wysota
11th June 2007, 09:07
You should set the root index in the view, not in the proxy. Don't touch the models, they are perfectly fine.

weepdoo
11th June 2007, 10:14
Thanks for your help.