Custom Table Model
-
@EaccB said in Custom Table Model:
Then what do I use to signal to the view that the model has been updated?
dataChanged() as explained above.
-
@EaccB said in Custom Table Model:
Then what do I use to signal to the view that the model has been updated?
dataChanged() as explained above.
Ok, I'm really confused. You said that I shouldn't use dataChanged() when removing a row. So, what should I do when an element is removed from the vector? Or are you saying that I should emit dataChanged from the class which owns the vector and connect it to a slot in the Model?
-
@EaccB said in Custom Table Model:
So, what should I do when an element is removed from the vector?
You really really should read the documentation. I already gave a the link two times...: https://doc.qt.io/qt-5/model-view-programming.html#inserting-and-removing-rows
-
Ok, I'm really confused. You said that I shouldn't use dataChanged() when removing a row. So, what should I do when an element is removed from the vector? Or are you saying that I should emit dataChanged from the class which owns the vector and connect it to a slot in the Model?
@EaccB
To reiterate what @Christian-Ehrlicher is telling you. The signal code in a model has to depend on whether you are updating an existing row which is already there and will remain there, versus if you inserting or deleting a row.dataChanged()is only for same rows, with values in columns changing.For inserting/deleting rows you must use
begin/endInsert/RemoveRows()instead.Views, for example, need to know this and react differently according as the model reports its rows are being updated or are being deleted/inserted. You can imagine the action they take will differ between these cases.
-
@EaccB
To reiterate what @Christian-Ehrlicher is telling you. The signal code in a model has to depend on whether you are updating an existing row which is already there and will remain there, versus if you inserting or deleting a row.dataChanged()is only for same rows, with values in columns changing.For inserting/deleting rows you must use
begin/endInsert/RemoveRows()instead.Views, for example, need to know this and react differently according as the model reports its rows are being updated or are being deleted/inserted. You can imagine the action they take will differ between these cases.
I think we got out wires crossed. By "update the vector", I meant add and remove elements, as well as editing existing items. This is why I was confused when @Christian-Ehrlicher suggested using dataChanged(), which he had previously told me not to use for removing elements.
If I add an element directly to the vector, do I still need to call insertRows() from the model?
-
@EaccB said in Custom Table Model:
which he had previously told me not to use for removing elements.
where?
If I add an element directly to the vector, do I still need to call insertRows() from the model?
Yes, as clearly written in the docs.
-
@EaccB said in Custom Table Model:
which he had previously told me not to use for removing elements.
where?
If I add an element directly to the vector, do I still need to call insertRows() from the model?
Yes, as clearly written in the docs.
You must not call dataChanged() when removing a row.
-
You must not call dataChanged() when removing a row.
@EaccB Sorry, I over read the not
This statement is correct.
-
I think we got out wires crossed. By "update the vector", I meant add and remove elements, as well as editing existing items. This is why I was confused when @Christian-Ehrlicher suggested using dataChanged(), which he had previously told me not to use for removing elements.
If I add an element directly to the vector, do I still need to call insertRows() from the model?
If you use the model-as-interface-on-existing-data
@EaccB said in Custom Table Model:
editing existing items
- The item that owns the data should emit a signal after the item is modified, the model will connect to this signal and emit
dataChanged
I meant add and remove elements
- The item that owns the data should emit a signal before the item is added/removed, the model will connect to this signal and call
beginInsertRows/beginRemoveRows - The item that owns the data should emit a signal after the item is added/removed, the model will connect to this signal and call
endInsertRows/endRemoveRows
- The item that owns the data should emit a signal after the item is modified, the model will connect to this signal and emit
-