如何在从 sqlite 数据库获取数据的 qtableview 上创建一个 qcheckbox



我有一个QTableView,显示来自sqlite数据库的数据。显然,我正在为所有可用的数据行创建一个复选框。问题是复选框不可选中,单击时显示以下错误。

if value.toBool():
    AttributeError: 'int' object has no attribute 'toBool'

下面是我得到的代码,但没有给出我所需的结果。我想突出显示已单击其复选框的行。

    self.table_model3 = CheckboxSqlModel(0)
    self.table_model3.setQuery("SELECT Name, Email FROM Individuals")
    self.tableView3.setModel(self.table_model3)
    self.tableView3.horizontalHeader().setStretchLastSection(True)
    self.tableView3.setShowGrid(False)
    self.table_model3.setHeaderData(0, QtCore.Qt.Horizontal, "All")
    self.table_model3.setHeaderData(1, QtCore.Qt.Horizontal, "Client Name")
    self.table_model3.setHeaderData(2, QtCore.Qt.Horizontal, "Email Address")
    self.tableView3.setStyleSheet('QHeaderView:section{Background-color:#cdcdcd; font-family: Arial Narrow; font-size: 15px; height: 30px;}')
    self.table_model3.insertColumn(0)
    self.tableView3.setColumnWidth(0, 30)

class CheckboxSqlModel(QtSql.QSqlQueryModel):
    def __init__(self, column):
        super(CheckboxSqlModel, self).__init__()
        self.column = column
        self.checkboxes = list() #List of checkbox states
        self.first = list() #Used to initialize checkboxes
    #Make column editable
    def flags(self, index):
        flags = QtSql.QSqlQueryModel.flags(self, index)
        if index.column() == self.column:
            flags |= QtCore.Qt.ItemIsUserCheckable
        return flags
    def data(self, index, role=QtCore.Qt.DisplayRole):
        row = index.row()
        if index.column() == self.column and role == QtCore.Qt.CheckStateRole:
            #Used to initialize
            if row not in self.first :
                index = self.createIndex(row, self.column)
                self.first.append(row)
                self.checkboxes.append(False)
                return QtCore.Qt.Unchecked
            #if checked
            elif self.checkboxes[row]:
                return QtCore.Qt.Checked
            else:
                return QtCore.Qt.Unchecked
        else:
            return QtSql.QSqlQueryModel.data(self, index, role)
    def setData(self, index, value, role=QtCore.Qt.DisplayRole):
        row = index.row()
        if index.column() == self.column and role == QtCore.Qt.CheckStateRole:
            if value.toBool():
                self.checkboxes[row] = True
            else:
                self.checkboxes[row] = False
            self.dataChanged.emit(index, index)
            return True
        else:
            return False

问题是值被转换为保存的类型,在您的情况下,它是一个整数,它采用以下值 Qt::CheckedQt::PartiallyCheckedQt::Checked ,解决方案是将它们与这些值进行比较。

def setData(self, index, value, role=QtCore.Qt.DisplayRole):
    row = index.row()
    if index.column() == self.column and role == QtCore.Qt.CheckStateRole:
        if value == QtCore.Qt.Checked:
            self.checkboxes[row] = True
        else:
            self.checkboxes[row] = False
        self.dataChanged.emit(index, index, [QtCore.Qt.CheckStateRole])
        return True
    else:
        return False

要选择或取消选择,我们使用以下方法

    self.table_model3.dataChanged.connect(self.on_data_changed)
def on_data_changed(self, topleft, bottomRight, roles):
    if QtCore.Qt.CheckStateRole in roles:
        row = topleft.row()
        isChecked = topleft.data(QtCore.Qt.CheckStateRole) == QtCore.Qt.Checked
        flag = QtCore.QItemSelectionModel.Select if isChecked else QtCore.QItemSelectionModel.Deselect
        for col in range(self.table_model3.columnCount()):
            ix = self.table_model3.index(row, col)
            self.tableView3.selectionModel().select(ix, flag)

相关内容

  • 没有找到相关文章

最新更新