hey thanks for the example code!
i messed around with the QModelView when trying to create an iTunes-
style navigation widget, but i remember having a lot of trouble
getting it to work. as i remember, a big part of the problem was
getting it to support the notion of the "All" item that appears at the
top of every column in iTunes. this is going on much-faded memory,
but perhaps it had something to do with QViewModel working more like a
tree, whereas iTunes behaves as a filtering system, where each
column's filter can be applied or not (the "not" being the "All"
item). after having mastered QModelView, do you have any insight into
this? do you know if QModelView could handle the slightly simpler --
and more tree-like -- case of a Finder-style column-based navigation?
-chad
On May 29, 2009, at 4:40 PM, 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
-~----------~----~----~----~------~----~------~--~---