Solved Need help to create simple model for QTreeModelView
-
@Please_Help_me_D
But when view ask how many items you got via rowCount, you return zero.
as in No rows and that is what you see. -
@mrjj Why do I return zero?
Are the comments below are true?int MyModel::rowCount(const QModelIndex &parent) const { if (!parent.isValid()) { // if this is a root (there is no parent) return _items.size(); // this returns 1 and the application goes here } const MyItem* parentItem = static_cast<const MyItem*>(parent.internalPointer()); // return parentItem->children.size(); //returns zero but the application never reach there }
-
@Please_Help_me_D Oh no, I'm wrong...
-
@Please_Help_me_D
well
you return parentItem->children.size()
and that is zero as it has no children :)So not sure if this is a tree or list or table model ?
-
@mrjj it should be tree model
-
@Please_Help_me_D
Then you should add more than the first item :)
So the root item does have children. -
@mrjj Well I add few children to item:
void MyModel::fetchRootDirectory() { MyItem* item = new MyItem("first_item"); item->children.push_back(new MyItem("first_child")); item->children.push_back(new MyItem("second_child")); _items.push_back(item); }
but it still doesn't work :)
-
@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 :)