I am trying to retrieve the editted data from a pyqt5 qtablewidget and am struggling to understand how to do this. My simple code is shown below:
class Delegate(QStyledItemDelegate):
def createEditor(self, parent, option, index):
DBL_MAX = 1.7976931348623157e308
editor = QDoubleSpinBox(parent, minimum=-DBL_MAX, maximum=DBL_MAX, decimals=323)
return editor
class TableView(QTableWidget):
def __init__(self, z, *args):
super(TableView, self).__init__(*args)
self.z = z
self.setz()
self.resizeColumnsToContents()
self.resizeRowsToContents()
delegate = Delegate(self)
self.setItemDelegate(delegate)
def setz(self):
horHeaders = []
for j, (key, values) in enumerate(sorted(self.z.items())):
horHeaders.append(key)
for i, value in enumerate(values):
newitem = QTableWidgetItem()
newitem.setData(Qt.EditRole, value)
self.setItem(i, j, newitem)
self.setHorizontalHeaderLabels(horHeaders)
def slot(self):
row= self.tableWidget.currentItem()
print(str(row))
def main(args):
z = {
"Let's Sum This Row 1": [0, 0],
"Let's Sum This Row 2": [0, 0],
}
app = QApplication(args)
table = TableView(z, 2, 2)
table.show()
sys.exit(app.exec_())
if __name__ == "__main__":
main(sys.argv)
a = z
sum1=a[0][0]+a[0][1]
sum2=a[1][0]+a[1][1]
print(sum1)
print(sum2)
In my simple example my initial z data is a zeros. Let's say that the user changes the first rows to 1's and the second row to 2's.
The end output I want is sum1 = 2 and sum2 = 4.
How do i tell my program to read the end user editted matrix and store that back to a variable so that I can do more work further on in my program?
From my research, it looks like itemchanged
may do the trick, but I am struggling to understand how to implement this in my code.