Search code examples
pythonpyqtselectionqtablewidget

Getting Selected Rows Count in QTableWidget - PyQt


In the python plugin, I'm developing, I need to retrieve the number of selected rows in a QTableWidget. I can loop through each row of the QTableWidget and check them whether it is selected or not. Instead, Is there a straightforward way to get the selected rows count of a QTableWidget in PyQt?

Something like:

QTableWidget.selectedRowsCount()


Solution

  • If you want the number of rows that are fully selected (i.e. as when clicking on a row header):

    len(tableWidget.selectionModel().selectedRows())
    

    But if you want rows which just have at least one cell selected:

    len(set(index.row() for index in tableWidget.selectedIndexes()))