[PyKDE] MVC Question

Matt Chambers chambers at imageworks.com
Wed Oct 11 21:26:27 BST 2006


Andreas Pakulat wrote:
> On 06.10.06 10:14:17, Matt Chambers wrote:
>   
>> Thanks, that works but then then any branches you have open in the tree view 
>> closed.   I guess there is no way to maintain
>> the open/close status of existing branches, and default new ones to open?
>>     
>
> Hmm, there's API to set the status of a given model index to expanded
> (in QTreeView), you'd have to store the indexes that are expanded.
>
> But I think there's no way to have new tree items opened by default,
> other than calling setExpanded after inserting new items. 
>
> But generally emitting modelReset() signal means the model's data
> completely changed and thus the view drops all information it stores.
>
> If you only want to insert a new node into the model, write functions
> that call beginInsertRows/endInsertRows. For more information see the
> Qt4 docs, there a chapter about model/view. 
>
> Andreas
>   
Keeping a record of which nodes I have open ending up being slow, and 
there were other problems with the full
refresh option that turned me off to it.  So I started to look into 
doing a single full update in the beginning, and
then pulling incremental updates which update my underlying data.

Question now is, I can update the data no problem.  How can I find out 
what indexes to use when emiting a dataChanged signal
if I update the underlying data in an elementtree ?


class TreeModel( QtCore.QAbstractItemModel ):
   
    def __init__(self, show ):
        QtCore.QAbstractItemModel.__init__(self)
        self.__columns = [ ]   
        self.__data = Data(show,self)
    
    def addColumn(self,label,tag):
        """addColumn(String label, string Tag)
       
        """
        self.__columns.append( [QtCore.QVariant(label),tag] )
  
    def data(self, index, role):

        if role != DISPLAY_ROLE:
            return EMPTY_VARIANT
       
        if not index.isValid():
            return EMPTY_VARIANT

        el = index.internalPointer()
        return QtCore.QVariant( el.get(self.__columns[index.column()][1]) )
   
    def headerData(self, section, orientation, role):
        if orientation == QtCore.Qt.Horizontal and role == DISPLAY_ROLE:
            return self.__columns[section][0]
        return EMPTY_VARIANT

    def index(self, row, column, parent):
        if parent.isValid():
            item = parent.internalPointer()
        else:
            item = self.__data.rootNode
           
        return self.createIndex(row, column, item[row])
   
    def parent(self, index):
        if not index.isValid():
            return EMPTY_INDEX
       
        item,row = self.__data.parentMap[index.internalPointer()]
       
        if id(item) == id(self.__data.rootNode):
            return EMPTY_INDEX
        else:
            return self.createIndex(self.__data.parentMap[item][1],0,item)
   
    def rowCount(self, parent):
        if parent.isValid():
            item = parent.internalPointer()
        else:
            item = self.__data.rootNode
       
        return len(item)
   
    def columnCount(self, parent):
        return len(self.__columns)


-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.riverbankcomputing.com/pipermail/pyqt/attachments/20061011/d815fbb1/attachment.html


More information about the PyQt mailing list