Qt Model View Programming: Creating indexes - qt

I am reading through following documentation to increase my understanding of model-view programming:
http://qt-project.org/doc/qt-4.8/model-view-programming.html
Although its nicely written, I have a question regarding creating indexes. As far as I understand to calculate and item's index we need index of its parent item. To calculate index of the parent item we need index of child item. So its a circular dependency. How do you break it?
If you go through following section:
Rows and columns
The diagram shows a representation of a basic table model in which each item is located by a pair of row and column numbers. We obtain a
model index that refers to an item of data by passing the relevant row
and column numbers to the model.
QModelIndex indexA = model->index(0, 0, QModelIndex());
QModelIndex indexB = model->index(1, 1, QModelIndex());
QModelIndex indexC = model->index(2, 1, QModelIndex());
Top level items in a model are always referenced by specifying QModelIndex() as their parent item. This is discussed in the next
section.
I do not understand how does Qt know to calculate index of A, parent index should be QModelIndex() (i.e. the index of top level item).

As far as I understand to calculate and item's index we need index of
its parent item.
Yes, unless it is a top level item.
To calculate index of the parent item we need index of child item.
No. An index cannot have more than one parent, so just call QModelIndex::parent() const.

Related

How to get model index inside QML TreeView rowDelegate?

I would like to set the height of each row in the treeview based on a model property, so I would need to have the model index to get it. But in rowDelegate, the index is not provided, only the row number, which is not correct for subitems.
https://doc.qt.io/qt-5/qml-qtquick-controls-treeview.html#rowDelegate-prop
Is there any way of getting this index, or setting the height in the itemDelegate (which has the styleData.index property)?

Get Item from QStandardItemModel for QTableView

I have a QTreeView in which each node represents a data object. I managed to pack a pointer to this data objects into a QVariant so that I know which is selected in the Tree. I can access the nodes by the currentIndex() function.
The root of the Tree is at QStandardItemModel::invisibleRootItem(). Other nodes are added by the parent nodes appendRow function. So the Items have no row and column that I can use in QStandardItemModel::item().
Is there a way to give the nodes valid coordinates or to retrieve the items without coordinates.
In QStandardItemModel, there is a item(int row, int column = 0) method. Qt nomenclature doesn't use 'get' as prefix of getters methods.

How do I Insert an item at top of QTreeView

In my app I'd like to insert an item at the top of a QTreeView.
What I have so far will insert an item just above the currently selected item. The code (nicked, I think, from the EditableTreeviewDemo):
QModelIndex index = this->selectionModel()->currentIndex();
QAbstractItemModel *model = this->model();
if (!model->insertRow(index.row(), index.parent()))
return;
I guess what I need is the index to the current first row? How do I get this?
As a side question, what happens to the current index when a row is inserted? Does it continue to point to the same item, or the same row?
Well first you have to know that insertRow is a function from QAbstractItemModel and it will call insertRows (with an s). This function must be redefined in your model subclass if you want to allow insertion of data in your model.
http://doc.qt.io/qt-5/qabstractitemmodel.html#insertRows
Also consider that any parent of a topmost index is a invalid QModelIndex. Then the call to do would be :
model->insertRow(0, QModelIndex());
And because this is the default value for the second parameter, simply call :
model->insertRow(0);
Then in your redefinition of insertRows simply check the validity of you parent index to ensure you news underlying data is created where you want it to be.
For you question, inserting data in the model won't affect the current and selected items.

Where is a row appended in QStandardItemModel?

There is a method
void QStandardItemModel::appendRow(QStandardItem * item)
in class QStandardItemModel. I cannot figure out where the item is appended, or who the item's parent is after appending?
And another method
bool QStandardItemModel::insertRow(int row, const QModelIndex & parent = QModelIndex())
shows the parent but which item will be appended to the parent?
The first question is easy. As the name says: Append. It is appended to a list or a tree, which has only one column.
The second one is difficult. This function is not directly about inserting items, but about the algorithm how to insert items. In a complicated tree this is not a necessarily trivial task. Adding nodes, changing the dimension of a tree. You have to implement this virtual method, when you have to create an editable tree. In that case making room for a new item and actually inserting the item are two different tasks.

QTreeView / QAbstractItemModel - adding items and using beginInsertRows

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.
Yes that's it.
Was this your only question?

Resources