Solved Need help to create simple model for QTreeModelView
-
@Please_Help_me_D
so check with debugger what you return as rowCount ? -
@mrjj I wrote:
int MyModel::rowCount(const QModelIndex &parent) const { if (!parent.isValid()) { qDebug() << "Item size: " << _items.size(); return _items.size(); } const MyItem* parentItem = static_cast<const MyItem*>(parent.internalPointer()); qDebug() << "Children size: " << parentItem->children.size(); return parentItem->children.size(); }
The output is:
Item size: 1 Item size: 1 Item size: 1 Item size: 1 Item size: 1 Item size: 1 Item size: 1 Item size: 1 Item size: 1 Item size: 1 Item size: 1 Item size: 1 Children size: 2 Item size: 1 Item size: 1 Item size: 1 Item size: 1 Item size: 1 Item size: 1 Children size: 2 Item size: 1 Item size: 1 Item size: 1
Why the program goes to
rowCount
so many times? I already suspected that when I set the breakpoint and tried to contue application run to next breakpoint. Is it normal? -
@Please_Help_me_D said in Need help to create simple model for QTreeModelView:
Why it gives me such many outputs?
Because your
rowCount()
gets called by Qt (e.g. theQTreeView
) more times than you think it does :) -
@JonB and is it normal? or my code looks so scarry that Qt don't know what to do whith it :)
-
@mrjj maybe I should manually insert rows somehow?
-
Hi
Im not sure what is wrong as could also be the index but you can take a look at
https://doc.qt.io/qt-5/qtwidgets-itemviews-editabletreemodel-example.html
which seems very similar to your code. -
@mrjj ok
How do you think, is it possible that the problem is how I declare variables (pointers/references and const)?. Sometimes they differ from examples but I think it should be fine -
@Please_Help_me_D
well i think its fine since app compiles etc.
Its more that if you use internalPointer as in the example so it does point to what you expect. -
@Please_Help_me_D said in Need help to create simple model for QTreeModelView:
@JonB and is it normal? or my code looks so scarry that Qt don't know what to do whith it :)
No, it's normal that infrastructure will call your
rowCount()
a lot :) -
@mrjj @JonB well guess what? Yes I've found the snippet of code that made me feel useless for whole day :D
Previously I had theMyModel::data()
method:QVariant MyModel::data(const QModelIndex &index, int role) const { if (!index.isValid()) { return QVariant(); } const MyItem* item = static_cast<MyItem*>(index.internalPointer()); return item->title; }
But when I change it to:
QVariant MyModel::data(const QModelIndex &index, int role) const { if (!index.isValid()) { return QVariant(); } if (role != Qt::DisplayRole && role != Qt::EditRole) return QVariant(); const MyItem* item = static_cast<MyItem*>(index.internalPointer()); return item->title; }
everything works :)
Now I'm trying to understand what the role is
Thank you for help! -
@Please_Help_me_D said in Need help to create simple model for QTreeModelView:
that made me feel useless for whole day
Ehh, should it not make you feel happy you found it ?
So good spotted.
The roles are explained here
https://doc.qt.io/qt-5/qt.html#ItemDataRole-enum -
@mrjj I would be if not my birthday today :D
-
@Please_Help_me_D
:)
Ok fair enough but it's also a bit hardcore to fiddle with QAbstractItemModel on
such a special day :) -
@mrjj thank you))
hardcore is my lastname :) -
@Please_Help_me_D said in Need help to create simple model for QTreeModelView:
Now I'm trying to understand what the role is
@mrjj has given you the documentation reference page for roles. You must read that! In Qt models, an index does not just store/return what you think of of as "the value" of that item, it also stores/returns a variety of additional data which you might want to set for each data item. Qt calls these "roles", I don't know where it gets that name from, you might think of them as "attributes".
When you implement your model's
data()
override, and alsosetData()
if you allow updates, you must make their behaviour take into account the value of therole
parameter. The Qt infrastructure (e.g. any views attached to the model) will be calling yourdata()
/setData()
methods a lot of times, with a variety of roles. For example, it will calldata(index, Qt::ForegroundRole)
for every cell to ask what color the text should be, ordata(index, Qt::ToolTipRole)
every time it wants to potentially show a tooltip.The normal pattern I follow for, say,
data()
is:QVariant MyModel::data(const QModelIndex &index, int role) const { if (!index.isValid()) return QVariant(); switch (role) { case Qt::DisplayRole: case Qt::EditRole: return somethingToDoWithWhatYouThinkOfAsTheValue; case Qt::AnotherRoleIWantToHandle: return somethingSuitable; default: break; } return BaseModel::data(index, role); }
And similarly for
setData()
. Note how we return the base class'sdata()
/setData()
method for any cases we don't handle; this allow for future updates. -
@JonB thank you for explanation! This is useful for me to know
-
@mrjj Once again I need help
I set two items to the root item. So here should be one openable item wich after unwrapping should show two other items... but it shows itself instead:void MyModel::fetchRootDirectory() { MyItem* item = new MyItem("first_item"); MyItem* item1 = new MyItem("first_child"); MyItem* item2 = new MyItem("second_child"); item->children.push_back(item1); item->children.push_back(item2); _items.push_back(item); }
Also there is something wrong with highlighting of chosen item
-
@Please_Help_me_D
Hi
I ran the updated code and it seems to return the same Modelindex for different items so
that's why you see the top one also as a child and both get selected if you click on one.
So i think something up with internalpointer but could not spot it.It seems like the code from
https://doc.qt.io/qt-5/qtwidgets-itemviews-editabletreemodel-example.html
But with some difference with the items and handling. -
@mrjj thank you!
I just created my model based on editabletreemodel and tried to make it as close as possible and it works now.That was the test model. Now I have to create my true model but here is the thing that I cannot tackle.
The main difference that was between my test model and editabletreemodel is that editabletreemodel hadTreeItem* rootItem
where were stored information about column header.
I want to have item that has a public variable that doesn't have the default constructorHighFive::File parentFile;
.
If I do my real model withrootItem
to store there information about column header then I will need to somehow initializeHighFive::File parentFile;
forrootItem
. ButrootItem
doesn't need to haveHighFive::File parentFile;
because this is a hdf5 file (I don't need it for columns header).
What would you suggest me?
I'm afraid to avoidrootItem
for header beacause I feel that this is the staff that made my test model useful. -
Hi
If you to stuff HighFive::File parentFile into an item, you can so do that with UserRoles.
each item can have extra data stored in user roles.QStandardItem *item = new xxx
item ->setData(value, Qt::UserRole + 1)
item ->setData(value2, Qt::UserRole + 2)
etcwhere value can be anything , including HighFive::File
So you can create and setup parentFile outside model and simply store it there.
If that is what you are asking about ?
Im not 100% sure what you need.