Unsolved Why doesn't QVariant work in this tree model's setData()?
-
Here's what I've got so far ...
QVector<QVariant> new_data; new_data << name << ""; const QModelIndex parent = tree_model->index(0, 0); MyTreeItem *new_item = new MyTreeItem(new_data, parent_item); tree_model->insertRow(0, parent); tree_model->setData(tree_model->index(0, 0, parent), QVariant(*new_item));
But I still can't get past this error ...
error: no matching function for call to ‘QVariant::QVariant(MyTreeItem&)’ tree_model->setData(tree_model->index(0, 0, parent), QVariant(*new_item)); ^
-
@SRaD
But why do you set Data with a new item ???
Are you trying to add extra data to the tree or trying to insert new items? -
I'm trying to add (insert) new child nodes to the tree? Am I using the wrong call?
-
@SRaD
Hi
Well you tried to use QVector<QVariant> new_data;
but it takes
TreeItem::TreeItem(const QList<QVariant> &data, TreeItem *parent)also, i had to surface rootItem to allow adding new items. Else only
void TreeModel::setupModelData(const QStringList &lines, TreeItem *parent)
seems the only way.anyway, i did
QList<QVariant> new_data; new_data << "test" << "test2"; TreeItem *new_item = new TreeItem(new_data, model.rootItem); model.rootItem->appendChild(new_item);
-
@SRaD said in Why doesn't QVariant work in this tree model's setData()?:
tree_model->insertRow(0, parent);
This does nothing for the
QAbstractItemModel
, so if you haven't you need to implement it.tree_model->setData(tree_model->index(0, 0, parent), QVariant(*new_item));
This should rather be:
int row = 0; // This is the index of the newly inserted row in the parent's table. It may differ from 0 for (int i = 0, size = new_data.size(); i < size; i++) tree_model->setData(tree_model->index(row, i, parent), new_data[i]);
-
@kshegunov
Just to be clear
The poster should implement setData and in there use RootItem
to add new TreeItem in there? -
No, the OP should already have either a pointer or an ID to the parent object which is stored in the model index. Whenever [s]he needs to add a new node that's what should be used as a parent. The model index itself is just an identifier to the data.
setData
you implement to drumroll ... set the data. If the data is coming from outside the model, then you just notify the connected proxy models and/or view by emitting the relevant signals - that is you callbeginInsertRows
/beginInsertColumns
anendInsertRows
/endInsertColumns
in the appropriate places. -
@kshegunov
Ok, but i don't see how the model will let you know the existing TreeItem you want to¨
append the new node to. (from outside)
Something like ?const QModelIndex parent = model.index(0, 0); TreeItem *itemParent = static_cast<TreeItem *>(parent.internalPointer());
-
@mrjj said in Why doesn't QVariant work in this tree model's setData()?:
Something like ?
Exactly like. However you already get the parent index from the Qt runtime, you don't create it with
QAbstractItemModel::index
. -
ok super.
So basically the poster would have more fun using the model
from
https://doc.qt.io/qt-5/qtwidgets-itemviews-editabletreemodel-example.html
as it already implemented setData and support for altering the data.