PyQt5 Table selected items doesn't include all selected items
12:56 02 Jun 2026

I have a QTableWidget and I want to select any number of cells, type in a new value, and have that value show up in all the selected cells. My code looks like this:

class NewTable(QDialog):
    def __init__(self, senv, lnewparms, parent=None):
        super().__init__(parent)
        ...
        # Create Table
        vlayout = QVBoxLayout()
        self.parmtbl = QTableWidget()
        vlayout.addWidget(self.parmtbl)
        self.parmtbl.setColumnCount(len(self.df.columns))
        self.parmtbl.setRowCount(len(self.df.index))
        self.parmtbl.setHorizontalHeaderLabels(self.df.columns.tolist())
        for index, row in self.df.iterrows():
            self.parmtbl.setItem(index, 0, QTableWidgetItem(row["NAME"]))
            self.parmtbl.item(index, 0).setFlags(Qt.ItemIsSelectable)
        self.parmtbl.cellChanged.connect(self.onmulticellchange)
        ...

    # Update all selected cells with entered value
    @pyqtSlot(int, int)
    def onmulticellchange(self, irow, icol):
        self.parmtbl.blockSignals(True)
        snewval = self.parmtbl.item(irow, icol).text()
        rc = self.oncellchange(irow, icol, snewval)
        print([x.row() for x in self.parmtbl.selectedItems()]) # Only 1 row in list
        for eachitem in self.parmtbl.selectedItems():
            if eachitem.row() == irow and eachitem.column() == icol:
                rc = 1
            elif rc:
                rc = self.oncellchange(eachitem.row(), eachitem.column(), snewval)
        self.parmtbl.blockSignals(False)

    # Update cell and DataFrame
    def oncellchange(self, irow, icol, snewval):
        self.df.iloc[irow, icol] = snewval
        self.parmtbl.item(irow, icol).setText(snewval)
        return 1

I have another class with almost identical code that works, that is all selected cells get changed. However, when running the above class, only one cell gets changed. I added the print statement to see the contents of self.parmtbl.selectedItems(), and it only contains 1 address despite the fact that I selected several cells. Thanks in advance!

python-3.x pyqt5