[PyQt] QDirModel instance
Scott Frankel
frankel at circlesfx.com
Tue Jul 22 07:24:16 BST 2008
Hello,
I'm having trouble getting checkboxes to work properly when I use a
sub-classed QDirModel and CheckStateRole in data(). The app's
checkboxes don't appear to respond to mouse-clicks. (Although their
states are changing, as I've verified in the model.)
If anyone has a moment to execute and/or review the small code example
I've copied below, I'd greatly appreciate it.
Thanks in advance!
Scott
[ executable example ]
#!/usr/bin/env python
import sys
from PyQt4 import QtCore, QtGui
class DirCheckForm(QtGui.QWidget):
def __init__(self, rootDir, parent=None):
QtGui.QWidget.__init__(self)
self.rootDir = rootDir
self.model = DirModel()
self.tree = QtGui.QTreeView()
self.tree.setModel(self.model)
if (self.rootDir == None) or (self.rootDir == ""):
self.tree.setRootIndex(self.model.index(QtCore.QDir.currentPath()))
else:
self.tree.setRootIndex(self.model.index(self.rootDir))
self.formLayout = QtGui.QVBoxLayout()
self.formLayout.addWidget(self.tree)
self.setLayout(self.formLayout)
class DirModel(QtGui.QDirModel):
def __init__(self, parent=None):
QtGui.QDirModel.__init__(self, parent)
self.checkstates = {}
def data(self, index, role=QtCore.Qt.DisplayRole):
if (role == QtCore.Qt.CheckStateRole and index.column() == 0):
self.checkstates[self.fileInfo(index).absoluteFilePath()] =
QtCore.Qt.Checked
return
QtCore
.QVariant(self.checkstates[self.fileInfo(index).absoluteFilePath()])
return QtGui.QDirModel.data(self, index, role)
def setData(self, index, value, role=QtCore.Qt.EditRole):
if (role == QtCore.Qt.CheckStateRole and index.column() == 0):
self.checkstates[self.fileInfo(index).absoluteFilePath()] =
QtCore.Qt.CheckState()
self
.emit(QtCore.SIGNAL("dataChanged(QtCore.QModelIndex,QModelIndex)"),
index, index)
return True
return QtGui.QDirModel.setData(self, index, value, role)
def flags(self, index):
return QtGui.QDirModel.flags(self, index) |
QtCore.Qt.ItemIsUserCheckable
if __name__ == "__main__":
app = QtGui.QApplication(sys.argv)
rootDir = "."
form = DirCheckForm(rootDir)
form.setWindowTitle("Test")
form.show()
sys.exit(app.exec_())
More information about the PyQt
mailing list