Delete a selected row in a QTableWidget

2

I have the following problem: I try to delete a selected row of a TableWidget but it does not work. This is the function I occupy:

self.check.clicked.connect(lambda:self.tabla.selectedItems().clear())

And this is the complete code:

from PyQt5.QtWidgets import *

from PyQt5 import uic 




class Principal(QMainWindow):
    def __init__(self):
        QMainWindow.__init__(self)
        uic.loadUi("columna.ui",self)

        self.tabla.insertRow(self.tabla.rowCount())
        self.tabla.setItem(self.tabla.rowCount()-1,2,QTableWidgetItem("sdsd"))
        self.tabla.setItem(self.tabla.rowCount()-1,1,QTableWidgetItem("sdsd"))
        self.tabla.insertRow(self.tabla.rowCount())
        self.tabla.setItem(self.tabla.rowCount()-1,1,QTableWidgetItem("sdsd"))
        self.boton.clicked.connect(lambda:self.tabla.setShowGrid(False)) #Limpiar sin dejar la rejilla en la tabla

        self.check.clicked.connect(lambda:self.tabla.selectedItems().clear()) #Función para eliminar solo una row




app = QApplication([])
p = Principal()
p.show()
app.exec_()

    
asked by Revsky01 17.07.2018 в 21:32
source

1 answer

2

QTableWidgetselectedItems returns a Python list with the selected items, so QTableWidgetselectedItems().clear() what it does is apply the list.clear() method, cleaning the returned list but nothing else.

If you want to delete the row of the current item you can get the row with QTableWidget.currentRow and use QTableWidget.removeRow to remove it:

self.check.clicked.connect(lambda:self.tabla.removeRow(self.tabla.currentRow()))

Keep in mind that this eliminates the entire row and its items, it is not limited to cleaning the contents of the item.

    
answered by 17.07.2018 / 23:21
source