Am having a Python program designed with PyQt where i use a QTableView to display data from a database. Am using the program to find specified matching values and then select them. After the items being highlighted, i want to be able to find the sum of the selected values in a specified column. Here is my code that am trying to use.
start = self.table_model2.index(0, 4)
matches = self.table_model2.match(start, QtCore.Qt.DisplayRole, code, -1, QtCore.Qt.MatchExactly)
for index in matches:
self.tableView2.selectionModel().select(index, QtGui.QItemSelectionModel.Select |
QtGui.QItemSelectionModel.Rows)
for index in sorted(self.tableView2.selectionModel().selectedRows()):
row = index.row()
self.payment = self.table_model2.data(self.table_model2.index(row, 9))
print(sum([int(self.payment)]))
You are converting to whole, then to a list and finally you add that unnecessary list, the last 2 tasks are unnecessary, the correct thing is to accumulate the values:
result = 0
for index in sorted(self.tableView2.selectionModel().selectedRows()):
payment = self.table_model2.data(self.table_model2.index(index.row(), 9))
result += int(payment)
print(result)