Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

How to enable checkbox activity in TableView



  • Hi!
    In PyQt, I made checkboxes in the TableModel using this flag (column 1):

        def data(self, index, role=None):
            if   index.column() == 0 and role == 0:
                return self.id[index.row()]
            elif index.column() == 1 and role == 10:
                return self.checked[index.row()]
            elif index.column() == 2 and role == 0:
                return self.nameCd[index.row()]
            return None
    

    How to enable checkbox activity in TableView?
    If I do this, the program crashes on the penultimate line

        def flags(self, index):
            result = super(ModelCdViewer, self).flags(index)
            print(result)
            if index.column() == 1 :
                result |= Qt.ItemIsUserCheckable
            return result
    

    It is too not works:

    self.ui.tableView.setEditTriggers(QtWidgets.QAbstractItemView.AllEditTriggers)
    


  • @Mikeeeeee said in How to enable checkbox activity in TableView:

    elif index.column() == 1 and role == 10:

    Why 10?

    The def flags() is what you need to enable checkboxes. Your code looks good to me. Try it again? :)



  • @JonB 10 because this is how the checkbox appears in the cell with true/false.
    How do I enable cell editing?



  • @Mikeeeeee
    OK, now I understand Qt.CheckStateRole == 10, might be better if you used the constant like you did for Qt.ItemIsUserCheckable.

    A moment ago you were asking

    How to enable checkbox activity in TableView?

    Now you say

    How do I enable cell editing?

    What is your question?



  • Me need enable checkbox activity in TableView.
    If I do this with checkboxes, then after clicking on the checkbox, the program breaks. Probably after changing the checkbox.

        def flags(self, index):
            result = super(ModelCdViewer, self).flags(index)
            print(result)
            if index.column() == 1 :
                result |= QtCore.Qt.ItemIsUserCheckable
                # result |= QtCore.Qt.ItemIsEditable
            return result
    

    If I do this with text input, then after changing the text, the program breaks down

        def flags(self, index):
            result = super(ModelCdViewer, self).flags(index)
            print(result)
            if index.column() == 2 :
                result |= QtCore.Qt.ItemIsEditable
            return result
    

    How do I save checkbox values?



  • I tried to do this, but the program still crashes. What else does the program need?

        def setData(self, index, value, role=QtCore.Qt.EditRole):
            if not index.isValid():
                return False
    
            if index.column() == 1:
                print(value)
                print(self.checked[index.row()])
                if self.checked[index.row()]:
                    self.checked[index.row()] = False
                else:
                    self.checked[index.row()] = True
                print(self.checked[index.row()])
                
                # self.checked[index.row()] = value
                self.dataChanged().emit(index, index)
                return True
    
            return False
    


  • it is work

        def flags(self, index):
            result = super(ModelCdViewer, self).flags(index)
            if index.column() == 1 :
                result |= QtCore.Qt.ItemIsUserCheckable
                # result |= QtCore.Qt.ItemIsEditable
            return result
    
        def setData(self, index, value, role=QtCore.Qt.EditRole):
            if not index.isValid():
                return False
    
            if index.column() == 1:
                print(value)
                print(self.checked[index.row()])
    
                if self.checked[index.row()]:
                    self.checked[index.row()] = False
                else:
                    self.checked[index.row()] = True
    
                print(self.checked[index.row()])
    
                # self.checked[index.row()] = value
                # self.dataChanged().emit(index, index)
                return True
    
            return False
    


  • @Mikeeeeee said in How to enable checkbox activity in TableView:

    self.dataChanged().emit(index, index)

    This is wrong. Should be

    self.dataChanged.emit(index, index)


Log in to reply