PyQt comes with a file called "modeltest.py" in the contrib directory 
that is a huge help in developing & debugging your custom 
QAbstractItemModel.  It's tricky to write one correctly, and 
modeltest.py runs it through a gauntlet of asserts to ensure that 
everything is behaving as it should.

Ben

thirstydevil wrote:
> Tonight I tried to play with QT's QModel concept.  It had stumped me
> for quite sometime at work today.  I couldn't rest untill I had
> understood the basics.  I'm not sure if this will be usefull to the
> hoi polloi but some may find it useful.
> 
> Anyways, heres a listView using a QDirModel for reference.
> Oh, and for those that are python/Qt gurus please comment if I'm doing
> this the wrong way.
> 
> -Cheers
> Dave
> 
> ## ----------------------------------------------
> 
> from PyQt4 import QtCore, QtGui
> import sys
> import os
> 
> class UI(QtGui.QWidget):
>     def __init__(self, parent=None):
>         QtGui.QWidget.__init__(self, parent)
> 
>         self.setWindowTitle('listView ModelView Example')
>         self.listView = MyListView()
>         self.upButton = QtGui.QPushButton()
>         self.upButton.setText("Up")
> 
>         grid = QtGui.QGridLayout()
>         grid.addWidget(self.upButton)
>         grid.addWidget(self.listView)
> 
>         self.setLayout(grid)
> 
>         self.resize(300, 150)
> 
>         items = self.listView.getModelItemCollection()
>         print self.listView.DirModel.getData(items)
> 
>         self.connect(self.upButton, QtCore.SIGNAL("clicked()"),
> self._GoUp)
> 
>     def _GoUp(self):
>         path = self.listView.DirModel.filePath(self.listView.rootIndex
> ())
>         self.listView.setRootIndex(self.listView.DirModel.index
> ( os.path.dirname(str(path))))
> 
>     @staticmethod
>     def Display():
>         app = QtGui.QApplication(sys.argv)
>         win = UI()
>         win.show()
>         sys.exit(app.exec_())
> 
> class MyDirModel(QtGui.QDirModel):
>     '''
>     SubClass of QtGui.QDirmodel
>     '''
>     def __init__(self):
>         super(MyDirModel, self).__init__()
> 
>     def getData(self, ModelIndex):
>         '''
>         Using QModelIndex I can get data via the data() method or
>         via any other method that suport a QModelIndex
>         '''
>         paths = []
>         if isinstance(ModelIndex, list):
>             for items in ModelIndex:
>                 #print self.data(items).toString()
>                 paths.append(self.filePath(items))
>             return paths
>         else:
>             raise ValueError("getData() requires a list
> (QtGui.QModelIndexs)")
> 
> class MyListView(QtGui.QListView):
>     '''
>     SubClass of QtGui.QListView
>     '''
> 
>     def __init__(self, parent = None):
>         super(MyListView, self).__init__(parent)
>         self.DirModel = MyDirModel()
>         self.setModel(self.DirModel) # we have to set the listView to
> use this DirModel
>         self.setRootIndex(self.DirModel.index("c://")) # set the
> deafault to load to c:\
> 
>         self.connect(self, QtCore.SIGNAL("doubleClicked
> (QModelIndex)"), self._DoubleClicked)
> 
>     def getModelItemCollection(self):
>         '''
>         From this list get the QModelIndex that we set
> with .setRootIndex
>         Using QModelIndex and DirModel we can get all the elements at
> that Index
>         '''
>         ModelIndex = self.rootIndex()
>         ModelIndexCollection = []
>         for i in range(0, self.DirModel.rowCount(ModelIndex)):
>             ModelIndexCollection.append(ModelIndex.child(i,0))
>         return ModelIndexCollection
> 
>     def _DoubleClicked(self):
>         if self.DirModel.isDir(self.currentIndex()):
>             path = self.DirModel.filePath(self.currentIndex())
>             self.setRootIndex(self.DirModel.index(path))
>             items = self.getModelItemCollection()
>             print self.DirModel.getData(items)
> 
> if __name__ == "__main__":
>     UI.Display()
> 
> 
> > 

--~--~---------~--~----~------------~-------~--~----~
http://groups.google.com/group/python_inside_maya
-~----------~----~----~----~------~----~------~--~---

Reply via email to