-1

I'm trying to do a GUI that can calculate multiple vectors operations (sum, rest, multiplication, division). I'm using the "QTableWidget" from "Qt Designer" and doing the code in spyder. But I can't find the way to perform the operations, I'm new using "Qt Designer".

enter image description here

enter image description here

I already got it to do the sums of the values, but now I have another problem, how could I sum the value of each column more efficiently?, since in this way I would have a very long code

def Defin(self):
    nv = self.spinBox.value()
    tv = self.spinBox_2.value()
    self.tableWidget.setColumnCount(tv)
    self.tableWidget.setRowCount(nv)
    self.tableWidget_3.setColumnCount(tv)
    self.tableWidget_3.setRowCount(1)
    self.tableWidget.setVerticalHeaderLabels(str("Vector 1;Vector 2;Vector 3;Vector 4;Vector 5;Vector 6;Vector 7;Vector 8;Vector 9;Vector 10",).split(";"))
    self.tableWidget_3.setVerticalHeaderLabels(str("Vector Resultante;",).split(";"))



def Operacion(self):
        cb = self.comboBox.currentText()
        if (cb=="Suma"):
            v1= self.tableWidget.item(0,0)
            v2= self.tableWidget.item(1,0)
            v1= v1.text()
            v2= v2.text()
            vri= int(v1) + int(v2)
            vri= str(vri)
            vri= QTableWidgetItem(vri)
            
            v3= self.tableWidget.item(0,1)
            v4= self.tableWidget.item(1,1)
            v3= v3.text()
            v4= v4.text()
            vri2= int(v3) + int(v4)
            vri2= str(vri2)
            vri2= QTableWidgetItem(vri2)
            self.tableWidget_3.setItem(0, 0, vri)
            self.tableWidget_3.setItem(0, 1, vri2)
JohnaMN
  • 1
  • 2
  • You need to iterate over the cells in the table, get their numeric values and then do the calculations. – mkrieger1 Nov 12 '22 at 22:35
  • Yeah, I know that I need to do that, but, I don't know and I can't find how to do it, I already do the code as a class without qt creator and it works – JohnaMN Nov 12 '22 at 22:42
  • Have you checked https://doc.qt.io/qtforpython-5/PySide2/QtWidgets/QTableWidget.html#PySide2.QtWidgets.PySide2.QtWidgets.QTableWidget.itemAt and https://doc.qt.io/qtforpython-5/PySide2/QtWidgets/QTableWidgetItem.html#PySide2.QtWidgets.PySide2.QtWidgets.QTableWidgetItem.text? – mkrieger1 Nov 12 '22 at 22:50
  • Yes, but for example, when I tried to sum, I get this error "TypeError: unsupported operand type(s) for +: 'QTableWidgetItem' and 'QTableWidgetItem'" – JohnaMN Nov 12 '22 at 22:56
  • That's why you need to take the text from the item first and convert it to a number. – mkrieger1 Nov 12 '22 at 22:58
  • Thank you very much, I was reading well what you put and it helped me a lot, it already worked – JohnaMN Nov 12 '22 at 23:37

1 Answers1

0

enter image description here

you can iterate over rows an columns and sum up like this:

def sum_up(self):
    from collections import defaultdict
    output = defaultdict(list)
    for col in range(self.table.columnCount()):
        for row in range(self.table.rowCount()):
            value = int(self.table.item(row, col).text())
            output[f'column_{col}'].append(value)

    print(output)
    print(sum(output['column_0']))
    print(sum(output['column_1']))

def sum_up_2(self):
    output = {}
    for col in range(self.table.columnCount()):
        for row in range(self.table.rowCount()):
            value = int(self.table.item(row, col).text())
            output[f'column_{col}'] = output.get(f'column_{col}', 0) + value

    print(output)
  • first function append all values of each column to a list and add it to dictionary which you can sum on list (defaultdict is from collections / from collections import defaultdict)
  • in second function we are adding the values and saving total in a dictionary

the result will be like this: enter image description here