Replicate the contents of the current selected item in QTableView



  • Hello,

    I have a QLineEdit that should be used to echo the contents of the current selected item in a QTableView. I already managed to do it when I enter an item using the QItemSelectionModel::selectionChanged signal, but when I start editing it and commit the changes by pressing the return key, as the selection has not changed, the contents of the QLineEdit and the QTableView get out of sync...

    So, first of all, is there a way to make the contents of these two (getting only the data from the EditRole of 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::closeEditor signal, 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.


  • Lifetime Qt Champion

    Hi,

    Sounds like QDataWidgetMapper could be of use to simplify all of that.

    Hope it helps



  • Do you think attaching my QLineEdit to 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::currentChanged from my QTableView to 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 the QDataWidgetMapper)


  • 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 QTableView and I want my QLineEdit to track the current cell. From what we can see in the case of that slot, the QDataWidgetMapper will 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 currentRowChanged with currentIndexChanged



Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.