Get Item from QStandardItemModel for QTableView - qt

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.

Related

Custom Model for TableView or QStandardItemModel

I want to create a TableView with two columns:
The first one for a parameter name
The second one with the parameter value, which could be a float, bool, int or a string
The second column should be editable, and a bool should be represented by a checkbox. For this model, should I subclass QAbstractItemModel or would the QStandardItemModel be fitting? Also, should one item have the two properties name and value, or should this be better seperated into two items?
It depends on your subject field. If you have big number of parameters then you should subclass QAbstractItemModel, because in the other case time of data view will be significant, if not QStandardItemModel is quite simple for this purpose.

How to find the index of a specific row in QMap based QAbstractListModel?

I have a class derived from QAbstractListModel based on a QMap<QUuid, CustomObject> which I am visualizing with ListView in qml. Some time during my applications running time I am removing some items from this map based on it's QUuid. When I am doing the removing I would like to call beginRemoveRows so the ListView is notified that it's content is changing and needs to redraw itself. How do I find out the right indexes for beginRemoveRows?
Actually using QMap is perfectly fine. You just have to find out the right indexes beginIndexRow, beginRemoveRows etc. For example finding the index for beginIndexRow looks something like this:
int index = std::distance(myQMap.begin(), myQMap.lowerBound(id));
beginInsertRows(QModelIndex(), index, index);
myQMap[id] = myCustomObject;
endInsertRows();

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.

Programmatically adding a new row to a QAbstractListModel subclass

Within an already-instantiated QAbstractListModel subclass, how do I add a row with data in each column, and have the associated QListView display the new row?
It seems that the only way to do it is to reimplement insertRow and setData within my model, and then hack them together in some sort of sequence within another function to add a row. Must I do this? Surely Qt must make it easier to add a new row.
Thanks much!
--Dany.
Just change your model's data storage, in between beginInsertRows() and endInsertRows().
For instance, let's say you have a flat list model and your model stores the data internally in a QVector m_data. You want to prepend the list, i.e. insert a row at position 0:
beginInsertRows( QModelIndex(), 0, 0 ); //notify views and proxy models that a line will be inserted
m_data.prepend( somedata ); // do the modification to the model data
endInsertRows(); //finish insertion, notify views/models
I'm afraid you have to do it that way. From the docs:
Models that provide interfaces to resizable list-like data structures can provide implementations of insertRows() and removeRows().

Resources