2

Following is the problem :

  • When I am editing a cell in QTableView,in middle of editing the udpates on the underlying model clears the cell and write the new Data.
  • I want that during the editing of the cell the new data change should not overwrite the cell [may be prevent updating that row]
  • My QTableView has a custom Model inheriting from QAbstractTableModel

I have already checked the relevant questions here but no help:

  1. QTableView: dataChanged event clears cell being edited
  2. QTableView with QStandardItemModel: How to perform live updates during editing a cell?
mapuna
  • 53
  • 1
  • 5
  • You could try the following (not sure if it'd help though): create a custom delegate subclassing [QStyledItemDelegate](https://doc.qt.io/qt-5/qstyleditemdelegate.html), use its methods [createEditor](https://doc.qt.io/qt-5/qstyleditemdelegate.html#createEditor]) and [setModelData](https://doc.qt.io/qt-5/qstyleditemdelegate.html#setModelData) to track the in-progress edit of model indexes. Each time the model updates during editing, [setEditorData](https://doc.qt.io/qt-5/qstyleditemdelegate.html#setEditorData) should be called - only update the editor data if the cell isn't being edited. – Dmitry Oct 18 '17 at 07:40
  • @Dmitry how can I know in the delegate if the cell is being edited , is there some signal which I can listen to in custom delegate. – mapuna Oct 18 '17 at 07:49
  • When the editing starts, `createEditor` is called. When the editing is about to end, `setModelData` is called. In between these calls (for particular model indexes) you can assume the cell is being edited. So `setEditorData` should be called after `createEditor` exactly once, when the initial data is being loaded to the editor from the model. If it's called for the second time after `createEditor` but before `setModelData`, it means the model's data got updated during editing and you should avoid changing the editor's data in that case. – Dmitry Oct 18 '17 at 08:04
  • @Dmitry thanks, after using your idea I can now prevent the rows from being updated , but this looks to be a very hackish/dirty way of doing things.Is there no other way to get notified when an Edit begins, bcoz currently I have to use -fpermissive or mutable variables in createEditor functions – mapuna Oct 18 '17 at 09:35
  • Well, I gave you my idea, I don't have any better one. Maybe someone else over here would. I would, however, advocate against using `-fpermissive` in favour of using mutable variables - while mutable variables are completely valid part of C++ language, `-fpermissive` flag makes the compiler accept code not conformant with the C++ standard. Avoid that at all cost. – Dmitry Oct 18 '17 at 09:58

0 Answers0