1

In my QTableView, I am trying to have first column of checkboxes but i am getting all the row columns filled with checkboxes and also all of them are checked by default. I am not able to changed the state of checkboxes either.

class MyTableModel(QtCore.QAbstractTableModel):
    def __init__(self, listData=[[]], headers=[], parent=None):
        super(MyTableModel, self).__init__(parent)

        self.__listData = listData
        self.__headers = headers


    def data(self, index, role):
        row = index.row()
        column = index.column()
        value = self.__listData[row][column]

        if role == QtCore.Qt.CheckStateRole:
            if self.__listData[row] == 0:
                return QtCore.QVariant(QtCore.Qt.Unchecked)
            else:
                return QtCore.QVariant(QtCore.Qt.Checked)
        if role == QtCore.Qt.EditRole:
            return value
        if role == QtCore.Qt.DisplayRole:
            return value
Ciasto piekarz
  • 7,853
  • 18
  • 101
  • 197
  • have you considered using a `QStandardItemModel` (populated by `QStandardItem`s) instead of creating your own model? It gives you a lot of this behaviour for free, and is recommended unless you have a specific need to create a custom model. – three_pineapples Sep 11 '15 at 07:25
  • well, I have not jumped directly to this stage, I have a specific need so for the rows that have checkbox checked gets to stay and unchecked what gets clear or not processed. – Ciasto piekarz Sep 11 '15 at 07:33
  • I get the requirements of the checkbox. I'm saying that functionality is implemented in `QStandardItemModel` already if you were to use it. My question is, do you need functionality which is not in `QStandardItemModel`? If you do, it is probably a good idea to detail them and include them in your code snippet so they can be incorporated into answers to the question. If you don't, then I would suggest switching to `QStandardItemModel` – three_pineapples Sep 11 '15 at 09:18

2 Answers2

1

Don't now about PyQT but think there is no difference with c++Qt. You need to override Qt::ItemFlags QAbstractItemModel::flags(const QModelIndex & index) const and return for your checkable column Qt::ItemIsUserCheckable.

0

Just came across this question. Not sure you found the answer. To answer your question, if you want the check boxes only for the first column, you will need return QtCore.Qt.Unchecked or QtCore.Qt.Checked only for index with column == 0. So, you can change your code to:

def data(self, index, role):
    row = index.row()
    column = index.column()
    value = self.__listData[row][column]

    if role == QtCore.Qt.CheckStateRole and column == 0:
        if self.__listData[row] == 0:
            return QtCore.QVariant(QtCore.Qt.Unchecked)
        else:
            return QtCore.QVariant(QtCore.Qt.Checked)
    if role == QtCore.Qt.EditRole:
        return value
    if role == QtCore.Qt.DisplayRole:
        return value

And, you can check if the index is in self.__listData and return QtCore.Qt.Unchecked or QtCore.Qt.Checked accordingly.

Harsh
  • 72
  • 1
  • 7