Perform operation when detecting the change in a QtableWidget item

3

I have a QTableWidget , of 3x5 in the item (5, 5) I do the sum of the number that enter inside the item (1,2) and (1,3).

Currently I need the user to click on a update button to be able to perform the update but I would like to know if there is a way to avoid this and that the operation is directly executed when a value changes.

How can I do so that the operation was carried out when detecting the change in value of any of the 2 items?

Code:

from PyQt5.QtWidgets import QMainWindow, QApplication
from PyQt5 import uic
from PyQt5.QtCore import Qt
from tabla import tabla_style

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

        self.tabla_1 #QtableWidgetInstanciado desde un archivo.ui


app = QApplication([])
p = Principal()
p.show()
app.exec_()
    
asked by Mystic_Force 01.09.2018 в 20:42
source

1 answer

2

You have two signals at your disposal in QtTableWidgets to detect the change of content in a cell of the table:

  • QTableWidget.itemChanged : Allows you to get the instance of QtTableWidget.Item whose data were modified.

  • QTableWidget.cellChanged : allows you to get the row and the column (indexes integers) of the cell in which is the item whose data were modified.

I leave an example of a table where the two signals are used, the slots only print the position of the cell, in them you can display the logic that allows you to do what you want to happen when you modify certain cells:

import sys
from PyQt5 import QtCore, QtGui, QtWidgets



class Ejemplo(QtWidgets.QMainWindow):
    def __init__(self, *args, **kwargs) -> None:
        super().__init__(*args, **kwargs)
        self.resize(690, 300)
        self.centralwidget = QtWidgets.QWidget(self)
        self.tableWidget = QtWidgets.QTableWidget(self.centralwidget)
        self.tableWidget.setGeometry(QtCore.QRect(10, 10, 680, 290))
        self.tableWidget.setColumnCount(5)
        self.tableWidget.setRowCount(3)

        self.tableWidget.setHorizontalHeaderLabels(['Col1', 'Col2', 'Col3', 'Col4', 'Col5'])
        self.tableWidget.setVerticalHeaderLabels(["A", "B", "C"])

        for i in range(3):
            for j in range(5):
                item = QtWidgets.QTableWidgetItem()
                item.setText(str(i + j))
                self.tableWidget.setItem(i, j, item)

        self.setCentralWidget(self.centralwidget)


        self.tableWidget.cellChanged.connect(self.on_cell_changed)
        self.tableWidget.itemChanged.connect(self.on_item_changed)

    @QtCore.pyqtSlot(int, int)
    def on_cell_changed(self, row: int, col: int) -> None:
        print(f"Se modificó la celda ({row}, {col})")

    @QtCore.pyqtSlot(QtWidgets.QTableWidgetItem)    
    def on_item_changed(self, item: QtWidgets.QTableWidgetItem) -> None:
        print(f"Se modificó el item en posición ({item.row()}, {item.column()})")



if __name__ == "__main__":
    app = QtWidgets.QApplication(sys.argv)
    window = Ejemplo()
    window.show()
    sys.exit(app.exec_())
    
answered by 01.09.2018 / 23:08
source