Replicate the contents of the current selected item in QTableView
-
Hello,
I have a
QLineEditthat should be used to echo the contents of the current selected item in aQTableView. I already managed to do it when I enter an item using theQItemSelectionModel::selectionChangedsignal, but when I start editing it and commit the changes by pressing the return key, as the selection has not changed, the contents of theQLineEditand theQTableViewget out of sync...So, first of all, is there a way to make the contents of these two (getting only the data from the
EditRoleof the current item) be always in sync and that works in both situations? If not, how do I solve this desynchronization issue?I thought I could solve this by listening for the
QAbstractItemDelegate::closeEditorsignal, but I don't know if it is a good idea, because I would have to cast the editor parameter to the correct type in the slot implementation, and it would put extraneous logic to it.Thank you.
-
-
Do you think attaching my
QLineEditto the current selected column and row, and updating it everytime the selection changes, is the way to go?I'm goingo to try it out! Thank you.
-
Thank you, @SGaist. I was able to do what I wanted with the class you suggested. Yay!!
I wonder if there was a simpler or more efficient solution, though.
Anyway, it is working the way I commented previously:
- I attached the
QItemSelectionModel::currentChangedfrom myQTableViewto a custom slot. - In this slot I remap the
QLineEdit's section to the new column (if it changed) and select the new row as the current index using theQDataWidgetMapper)
- I attached the
-
You normally use it as in the example in this doc: http://doc.qt.io/qt-5/qdatawidgetmapper.html#setCurrentModelIndex
-
You normally use it as in the example in this doc: http://doc.qt.io/qt-5/qdatawidgetmapper.html#setCurrentModelIndex
@VRonin said in Replicate the contents of the current selected item in QTableView:
You normally use it as in the example in this doc: http://doc.qt.io/qt-5/qdatawidgetmapper.html#setCurrentModelIndex
Yes, but keep in mind that I'm using a
QTableViewand I want myQLineEditto track the current cell. From what we can see in the case of that slot, theQDataWidgetMapperwill only update either the column or the row, depending only on its orientation.So, as I have to respond to changes in both column and row indices, I chose to do it in the way I described so I only connect to one signal and not two.
-
@VRonin said in Replicate the contents of the current selected item in QTableView:
You normally use it as in the example in this doc: http://doc.qt.io/qt-5/qdatawidgetmapper.html#setCurrentModelIndex
Yes, but keep in mind that I'm using a
QTableViewand I want myQLineEditto track the current cell. From what we can see in the case of that slot, theQDataWidgetMapperwill only update either the column or the row, depending only on its orientation.So, as I have to respond to changes in both column and row indices, I chose to do it in the way I described so I only connect to one signal and not two.
@Arthur-Araruna said in Replicate the contents of the current selected item in QTableView:
both column and row indices
All you need to do is replace
currentRowChangedwithcurrentIndexChanged