[PyQt] Checkboxs not checking

Cristobal Infante cgcris at gmail.com
Thu Nov 1 14:23:57 GMT 2012


Hi guys,

I've been battling for the last day to try and get a checkbox inside a
tableview to be checkable..
I've looked all over the net and still my checkbox don't check.

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)

Can somebody please have a look at this code and tell me why it may not be
working?

Thanks in advance,
Cris


class EditableShoSqlModel(QtSql.QSqlQueryModel):
    def __init__(self, parent=None):
        super(EditableShoSqlModel, self).__init__(parent)

    def flags(self, index):
        flags = super(EditableShoSqlModel, self).flags(index)
        if index.column() in (12):
            flags |= QtCore.Qt.ItemIsEditable |
QtCore.Qt.ItemIsUserCheckable | Qt.ItemIsEnabled
        return flags

    def data(self, index, role=QtCore.Qt.DisplayRole):
        value2 = super(EditableShoSqlModel, self).data(index)
        if index.column() == 12 and role == QtCore.Qt.CheckStateRole:
            if value2 != 0:
                return QtCore.Qt.Checked
            else:
                return QtCore.Qt.Unchecked
        return QtSql.QSqlQueryModel.data(self, index, role)


    def setData(self, index, value, role):
        if index.column() not in (12):
            return False

        primaryKeyIndex = self.index(index.row(), 0)
        id = self.data(primaryKeyIndex)

        if index.column() == 12 and role == QtCore.Qt.CheckStateRole:
            value2 = super(EditableShoSqlModel, self).data(index)
            if value2 != 0:
                ok = self.setAniStatus(id, value)
                return QtCore.Qt.Unchecked
            else:
                ok = self.setAniStatus(id, value)
                return QtCore.Qt.Checked
            return True
        self.refresh()
        return ok

    def refresh(self):
        MainWindow.AllShotsFromProject ()

    def setAniStatus(self, id, Number):
        mon = Number
        if mon != False:
           mon = 1
        else:
           mon = 0
        query = QtSql.QSqlQuery()
        sql = "UPDATE Shot SET Ani= '{0}' WHERE idShot =
{1}".format(int(mon), id.toString())
        query.prepare(sql)
        print 'lastQuery:', query.lastQuery()
        return query.exec_()
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.riverbankcomputing.com/pipermail/pyqt/attachments/20121101/c9303a21/attachment.html>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: checkbox.jpg
Type: image/jpeg
Size: 14767 bytes
Desc: not available
URL: <http://www.riverbankcomputing.com/pipermail/pyqt/attachments/20121101/c9303a21/attachment.jpg>


More information about the PyQt mailing list