Hi guys,<br><br>I've been battling for the last day to try and get a checkbox inside a tableview to be checkable..<br>I've looked all over the net and still my checkbox don't check.<br><br>Also, the checkbox is
reading the value (0 or 1) from a sql database correctly, but it's showing me this value (which I dont want)<br><br>Can somebody please have a look at this code and tell me why it may not be working?<br><br>Thanks in advance,<br>
Cris<br><br><br>class EditableShoSqlModel(QtSql.QSqlQueryModel):<br> def __init__(self, parent=None):<br> super(EditableShoSqlModel, self).__init__(parent)<br><br> def flags(self, index):<br> flags = super(EditableShoSqlModel, self).flags(index)<br>
if index.column() in (12):<br> flags |= QtCore.Qt.ItemIsEditable | QtCore.Qt.ItemIsUserCheckable | Qt.ItemIsEnabled<br> return flags<br> <br> def data(self, index, role=QtCore.Qt.DisplayRole):<br>
value2 = super(EditableShoSqlModel, self).data(index)<br> if index.column() == 12 and role == QtCore.Qt.CheckStateRole:<br> if value2 != 0:<br> return QtCore.Qt.Checked<br> else:<br>
return QtCore.Qt.Unchecked<br> return QtSql.QSqlQueryModel.data(self, index, role)<br> <br><br> def setData(self, index, value, role):<br> if index.column() not in (12):<br> return False<br>
<br> primaryKeyIndex = self.index(index.row(), 0)<br> id = self.data(primaryKeyIndex)<br> <br> if index.column() == 12 and role == QtCore.Qt.CheckStateRole:<br> value2 = super(EditableShoSqlModel, self).data(index)<br>
if value2 != 0:<br> ok = self.setAniStatus(id, value)<br> return QtCore.Qt.Unchecked<br> else:<br> ok = self.setAniStatus(id, value)<br> return QtCore.Qt.Checked<br>
return True<br> self.refresh()<br> return ok<br> <br> def refresh(self):<br> MainWindow.AllShotsFromProject ()<br><br> def setAniStatus(self, id, Number):<br> mon = Number<br>
if mon != False:<br> mon = 1<br> else:<br> mon = 0 <br> query = QtSql.QSqlQuery()<br> sql = "UPDATE Shot SET Ani= '{0}' WHERE idShot = {1}".format(int(mon), id.toString())<br>
query.prepare(sql)<br> print 'lastQuery:', query.lastQuery()<br> return query.exec_()<br> <br><br>