I’ve been facing a problem for several days now. I’m using a QTableView to display data from a model. I activated the complete line selection when the user click on a cell, to make the interface user friendly:
self.tableau.selectRow(element.row())
But, when the user presses F2, I would like to edit only the column 1. So, the expected behavior is:
- if I selected the column 4, this one is not editable
- if I press F2 when column 4 is selected, the column 1 is edited
But with the complete row selection, F2 can not know which cell is selected. So, I re-implemented the event handler:
def keyPressEvent(self, e):
"""It reimplements event management in this method """
# TODO: find a way to re-select the cell after editing
# We get the name of current file
nom_courant = self.tableau.model().index(self.row_selected, 1).data()
print(nom_courant)
if e.key() == QtCore.Qt.Key_F2:
try:
# We edit the cell name of the selected line
#http://stackoverflow.com/questions/8157688/specifying-an-index-in-qtableview-with-pyqt
self.tableau.edit(self.tableau.model().index(self.row_selected, 1))
except:
print("Pas de cell sélectionnée")
pass
# It retrieves the new file name. CAN NOT do in the
# if the model is not updated yet.
nouveau_nom = self.tableau.model().index(self.row_selected, 1).data()
print(nouveau_nom)
# Call the fct renaming only if the name has changed
if nom_courant != nouveau_nom:
#liste.renameFile(self.current_video, self.tableau.model().index(self.row_selected, 1).data())
print("entropie")
The matter now is this line:
self.tableau.edit(self.tableau.model().index(self.row_selected, 1))
I have no way to detect the end of the edition of the QLineEdit generated, and I need it to perform actions on the new content of the cell edited, because nouveau_nom is not updated if no keyboard event occurs.
Do you have an idea about how to get the end edition signal ?
(Please forgive my english, I’m french…)
First, you don’t need to actually intercept and change the cell selection to a row selection. You can just set the behavior on the view:
This will automatically select rows.
When you use a custom QLineEdit widgets in your table, then you need to connect QLineEdit.editingFinished() to whatever handler you want. Most likely you want it to call
dataChangedon your model.