How to submit data to the QSqlTableModel that has been changed by the user on the QTableView

428 Views Asked by At

I'm trying to retrieve the input from the user when modifying the QTableView as seen in the photo and then updating the QSqlTableModel in order to get my database modified.

photo

The problem is that QTableView has no signals as QTableWidget for example, "itemChanged", or "currentItemChanged", so it's difficult for me to capture the value that the user introduces, cause I can't get that signal, it doesn't exist in QTableView? How do I recognize that moment?

I must say I've managed to use the signal "selectionChanged" making QSqlTableModel a selectionModel, as I've understood. But that signal only gets me a QModelIndex where the focus is, and gets me the actual value of the cell, not the introduced by the user after clicking and editing the cell...

This is my code:

self.model = QSqlTableModel(db=db)
self.model.setEditStrategy(QSqlTableModel.OnFieldChange)
self.ui.projectSelector_tableView.setModel(self.model)
header = self.ui.projectSelector_tableView.horizontalHeader()
header.setSectionResizeMode(QtWidgets.QHeaderView.ResizeToContents)
self.model.setTable("tablefromdb")
self.model.select()

self.ui.projectSelector_tableView.selectionModel().selectionChanged.connect(self.selChanged)
self.projectSelectorWindow.show()


def selChanged(self, a, b):
    self.currentindex = a.indexes()[0]
    self.previousindex = b.indexes()[0]

def submitChanges(self):
    self.model.setData(self.currentindex, self.value)
    self.model.submitAll()

At the end of the day, I want to get the "self.value" that the user has typed in, and setData it to the QSqlTableModel, and I actually have the QModelIndex where it goes, but not the value.

I hope anyone can help me, thanks you all!

1

There are 1 best solutions below

0
mugiseyebrows On

Model has dataChanged(topLeftIndex, bottomRightIndex) signal, model emits this signal every time user edits anything, you can get value from index: topLeftIndex.data().