0

Am having a QTableView that shows data from an sqlite database. Apparently, am creating a checkbox on all available rows of data. The problem is that the checkboxes are not checkable and when clicked show the following error.

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

Below is the code that i got but not giving my required result. I would like to highlight the row whose checkbox has been clicked.

    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
eyllanesc
  • 235,170
  • 19
  • 170
  • 241
And3r50n 1
  • 337
  • 1
  • 5
  • 21

1 Answers1

1

The problem is that value is converted to the type that is saved, in your case it is an integer that takes the following values Qt::Checked, Qt::PartiallyChecked and Qt::Checked, the solution is to compare them with those values.

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

To select or deselect we use the following method

    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)
eyllanesc
  • 235,170
  • 19
  • 170
  • 241
  • now the check boxes are check-able but the corresponding rows are not getting highlighted – And3r50n 1 Mar 01 '18 at 19:16
  • @And3r50n1 Explain, what do you mean by *but the corresponding rows are not getting highlighted*? – eyllanesc Mar 01 '18 at 22:43
  • when the checkbox is clicked, i would like to see the whole of that row selected – And3r50n 1 Mar 02 '18 at 02:29
  • @And3r50n1 and what happens if another row is previously selected? the previous row is deselected or only the new row is selected. – eyllanesc Mar 02 '18 at 02:41
  • if the previous row is still checked, i would like to extend the selection. – And3r50n 1 Mar 02 '18 at 02:59
  • @And3r50n1 In other words, you want them to select the rows where the checkboxes are marked, but what happens if the user deselects it. I will place a code that will select the row when it is checked, and deselect if it is unchecked regardless of the previous selection. – eyllanesc Mar 02 '18 at 03:02
  • @And3r50n1 I have added the new method, but you must change `self.dataChanged.emit(index, index)` to `self.dataChanged.emit(index, index, (QtCore.Qt.CheckStateRole,))` in setData() – eyllanesc Mar 02 '18 at 18:57
  • i get the folowing error `self.dataChanged.emit(index, index, (QtCore.Qt.CheckStateRole,)) TypeError: QAbstractItemModel.dataChanged[QModelIndex, QModelIndex, list-of-int].emit(): argument 3 has unexpected type 'tuple` – And3r50n 1 Mar 03 '18 at 18:54
  • thanks for the great effort. that solved the problem – And3r50n 1 Mar 04 '18 at 02:55