PyQt QAbstractListModel seems to ignore tristate flags
Posted
by mcieslak
on Stack Overflow
See other posts from Stack Overflow
or by mcieslak
Published on 2010-04-15T01:35:27Z
Indexed on
2010/04/16
22:53 UTC
Read the original article
Hit count: 258
I've been trying for a couple days to figure out why my QAbstractLisModel won't allow a user to toggle a checkable item in three states. The model returns the Qt.IsTristate
and Qt.ItemIsUserCheckable
in the flags() method, but when the program runs only Qt.Checked and Qt.Unchecked are toggled on edit.
class cboxModel(QtCore.QAbstractListModel):
def __init__(self, parent=None):
super(cboxModel, self).__init__(parent)
self.cboxes = [
['a',0],
['b',1],
['c',2],
['d',0]
]
def rowCount(self,index=QtCore.QModelIndex()):
return len(self.cboxes)
def data(self,index,role):
if not index.isValid: return QtCore.QVariant()
myname,mystate = self.cboxes[index.row()]
if role == QtCore.Qt.DisplayRole:
return QtCore.QVariant(myname)
if role == QtCore.Qt.CheckStateRole:
if mystate == 0:
return QtCore.QVariant(QtCore.Qt.Unchecked)
elif mystate == 1:
return QtCore.QVariant(QtCore.Qt.PartiallyChecked)
elif mystate == 2:
return QtCore.QVariant(QtCore.Qt.Checked)
return QtCore.QVariant()
def setData(self,index,value,role=QtCore.Qt.EditRole):
if index.isValid():
self.cboxes[index.row()][1] = value.toInt()[0]
self.emit(QtCore.SIGNAL("dataChanged(QModelIndex,QModelIndex)"),
index, index)
print self.cboxes
return True
return False
def flags(self,index):
if not index.isValid():
return QtCore.Qt.ItemIsEditable
return QtCore.Qt.ItemIsEnabled | QtCore.Qt.ItemIsEditable | QtCore.Qt.ItemIsUserCheckable | QtCore.Qt.ItemIsTristate
You can test it with this,
class MainForm(QtGui.QMainWindow):
def __init__(self, parent=None):
super(MainForm, self).__init__(parent)
model = cboxModel(self)
self.view = QtGui.QListView()
self.view.setModel(model)
self.setCentralWidget(self.view)
app = QtGui.QApplication(sys.argv)
form = MainForm()
form.show()
app.exec_()
and see that only 2 states are available. I'm assuming there's something simple I'm missing. Any ideas? Thanks!
© Stack Overflow or respective owner