PyQt:tableview中的复选框委托

时间:2012-10-30 17:28:34

标签: pyqt pyqt4 qtableview qitemdelegate

我已经能够在我的tableview中编辑我的复选框。 它们出现在我的tableview中,但我不能对它们做任何事情。 就像标志没有正确设置一样。

class CheckBox(QtGui.QStyledItemDelegate):

    def __init__(self, parent = None):
        QtGui.QGraphicsWidget.__init__(self)

    def flags(self, index):
        return QtCore.Qt.ItemIsEditable | QtCore.Qt.ItemIsUserCheckable | Qt.ItemIsEnabled

    def paint (self, painter, option, index):

        item = QtGui.QTableWidgetItem()
        item.setFlags(QtCore.Qt.ItemIsUserCheckable | QtCore.Qt.ItemIsEnabled)

        # picks up data from database
        checked = index.model().data(index, Qt.EditRole).toBool()

        # Then draw the checkbox
        box = QtGui.QStyleOptionButton()

        box.palette = option.palette
        box.rect = option.rect
        box.state = QtGui.QStyle.State_Enabled

        if checked:
            box.state |= QtGui.QStyle.State_On
        else:
            box.state |= QtGui.QStyle.State_Off

        style = QtGui.QApplication.instance().style()
        style.drawControl(QtGui.QStyle.CE_CheckBox, box, painter)
        painter.restore()

嗨,这是基于Avaris评论的已审核代码,使用checkrole代替委托。但我仍然可以解决这个问题。我无法在复选框上打勾,而是向我显示了一个旋转框。它正在从数据库中正确读取值(0或1),但我想隐藏此值。任何帮助将不胜感激!

enter image description here

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)
        #self.clear()

        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
        print "ANIS"            
        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_()

1 个答案:

答案 0 :(得分:2)

if index.column() not in (12):无效。 (12)12相同,您无法foo in 12。那是TypeError。你可以foo in (12,),但没有必要。这很简单foo == 12

setData应返回TrueFalse,表示success,但您将返回QtCore.Qt.UncheckedQtCore.Qt.Checked。那不太合适。

如果您不想展示0/1,则应在data中执行此操作,只需返回None DisplayRole

这应该做你想要的:

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() == 12:
            flags |= QtCore.Qt.ItemIsEditable | QtCore.Qt.ItemIsUserCheckable | QtCore.Qt.ItemIsEnabled
        return flags


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

        return super(EditableShoSqlModel, self).data(index, role)


    def setData(self, index, value, role):
        if index.column() == 12 and role == QtCore.Qt.CheckStateRole:
            # `id` is a built-in function
            id_ = self.data(self.index(index.row(), 0)).toString()
            state, ok = value.toInt()
            if state == QtCore.Qt.Checked:
                value = 1
            else:
                value = 0
            result = self.setAniStatus(id_, value)
            if result:
                # we modified the db, we need to query again
                self.refresh()
            return result

        # I don't know what `refresh` does. But normally you don't need that
        return False


    def refresh(self):
        # something that re-queries the model.
        #self.setQuery('SELECT * FROM Shot')


    def setAniStatus(self, id_, value):          
        query = QtSql.QSqlQuery()
        sql = "UPDATE Shot SET Ani= {0} WHERE idShot = {1}".format(value, id_)
        query.prepare(sql)
        print 'lastQuery:', query.lastQuery()
        return query.exec_()