samedi 26 septembre 2020

How to delete rows in a QtableWidgit(Pyqt5) using Python which are checked?

I'm trying to delete rows which are checked in column 4: TodoApp

Here is a code:

 def AddActivity(self,columns):
    task = self.Input_task.text()
    date = self.date.text()
    duedate = self.due_date.text()

    chkBoxItem = QtWidgets.QTableWidgetItem()
    chkBoxItem.setFlags(QtCore.Qt.ItemIsUserCheckable | QtCore.Qt.ItemIsEnabled)
    chkBoxItem.setCheckState(QtCore.Qt.Unchecked)
    chkBoxItem.setData(LastStateRole, chkBoxItem.checkState())

       
    rowPosition = self.Activities_table.rowCount()
    self.Activities_table.insertRow(rowPosition)

    self.Activities_table.setItem(rowPosition, 0, QtWidgets.QTableWidgetItem(task))
    self.Activities_table.setItem(rowPosition, 1, QtWidgets.QTableWidgetItem(date))
    self.Activities_table.setItem(rowPosition, 2, QtWidgets.QTableWidgetItem(duedate))
    self.Activities_table.setItem(rowPosition, 3, chkBoxItem)

Input data into QtableWidgit

Clear all checked button:

self.clear_all_checked_button.clicked.connect(self.delete)

Delete Button

And finally function which deletes all checked rows:

def delete(self):
    for i in range(self.Activities_table.rowCount()):
        item = self.Activities_table.item(i,3)
        
        if item.checkState() == QtCore.Qt.Checked:
           it = self.Activities_table.item(i,3)
           del it

Delete Function

However I receive this message: in delete if item.checkState() == QtCore.Qt.Checked: AttributeError: 'NoneType' object has no attribute 'checkState'

Is there any way to fix this code so it works properly.




Aucun commentaire:

Enregistrer un commentaire