4

I'm implementing my model based on QAbstractItemModel and I'm using it with QTreeView to display hierachical data. Data are stored in sqlite table.

My question is how should I call beginInsertRows when adding subnodes. Lets say I have some parent node, it contains 10 subnodes. And I want to add new subnode (at the end).

I'm doing it like this:

beginInsertRows(parentIndex, currentNodesCount, currentNodesCount);
// actual inserting
endInsertRows()

currentNodesCount contains value 10 which is the number of rows in this subnode. The new node will be placed at the 11th position (10th counting from 0).

Is this logic correct ?

Thanks for help.


I'm wondering also about using beginRemoveRows.

Is this correct:

beginRemoveRows(parentIndex, currentRow, currentRow);
// delete record
endRemoveRows();

currentRow contains position in the list of the removed node counting from 0.

1 Answers1

3

Yes that's it.

Was this your only question?

ixM
  • 1,244
  • 14
  • 29
  • Yes thanks. How do I refresh the view after modifiing the record ? emit dataChanged(.., ..); ? –  Sep 13 '11 at 17:17
  • You will find a nice guide about the subclassing of models in the Qt doc ( http://doc.qt.nokia.com/latest/model-view-programming.html#model-subclassing-reference ). As indicated in the section "resizable models", in most of the case begin**() and end**() should be sufficient to tell the connected view (or any other component) that the data structure as changed. P.S. Do not hesitate to flag your question as "answered" if it is the case – ixM Sep 14 '11 at 15:37
  • @SteveLorimer Update to the link: http://doc.qt.io/qt-5/model-view-programming.html#model-subclassing-reference – Random Citizen Jan 25 '16 at 21:05