-1

I'm making a project using python where it can be like excel so when i input some numbers in my cell, it will automatically calculated

From my previous question I have tried some codes again but stuck when I tried to calculate current column value automatically using some previous column value.

Codes for calculating PGA value (same with IKS, just a little bit different) ...

def on_itemChanged(self, item):

    if item.column() in (0, 1):
        self.calculate_pga(item.row())

def calculate_pga(self, row):
    self.tablewidget.blockSignals(True)
    for col in (0, 1):
        ut = self.tablewidget.item(row, col)
        if ut is None:
            ut = QtWidgets.QTableWidgetItem("0")
            self.tablewidget.setItem(row, col, ut)
    self.tablewidget.blockSignals(False)
    ut_x = self.tablewidget.item(row, 0)
    ut_y = self.tablewidget.item(row, 1)
    x = float(ut_x.text())
    y = float(ut_y.text())
    pga =  x + y 
    ut_pga = self.tablewidget.item(row, 4)
    if ut_pga is None:
        ut_pga = QtWidgets.QTableWidgetItem()
        self.tablewidget.setItem(row, 4, ut_pga)
    ut_pga.setText(str(pga))

What is the proper way so I can get GSS value when the formula is (IKS * PGA)? This is the table

meovvis
  • 29
  • 6

1 Answers1

0
        # Find indexes of column PGA, IKS and GSS   
        headercount = self.dlg.tableWidget.columnCount()

        for x in range(headercount):
           headertext = self.tableWidget.horizontalHeaderItem(x).text()
           if 'PGA' == headertext:
                self.matchcol_pga = x
           if 'IKS' == headertext:
                self.matchcol_iks = x
           if 'GSS' == headertext:
                self.matchcol_gss = x
        # Set signal and call function on itemChanged 
        self.tableWidget.itemChanged.connect(self.changeResult)

   def changeResult(self, item):
       row = item.row()
       self.dlg.tableWidget.blockSignals(True)
       try:
           pga = self.dlg.tableWidget.item(row, self.matchcol_pga).text()  # get cell at row, col
           iks = self.dlg.tableWidget.item(row, self.matchcol_iks).text()  # get cell at row, col

           gss = int(pga) * int(iks)

           self.dlg.tableWidget.setItem(row, self.matchcol_gss, QTableWidgetItem(str(gss)))
       except:
           pass
       self.dlg.tableWidget.blockSignals(False)

NOTE: use blockSignals to avoid - returns error: RuntimeError: maximum recursion depth exceeded while calling a Python object

ncica
  • 7,015
  • 1
  • 15
  • 37