Solved Need help to create simple model for QTreeModelView
-
Hi,
I think I understood now how
QAbstractItemModel
work, of course I have already run few examples and now I'm trying to write my simple model with my items.
I can launch it but I can see only header of the column (like on the picture) but there also should be one item. So the problem is that I don't see my item.
I upload the project here
Please take a look at it.
-
@Please_Help_me_D
:)
Ok fair enough but it's also a bit hardcore to fiddle with QAbstractItemModel on
such a special day :) -
Need help to create simple model for QTreeModelView
Did you look at the Simple Tree Model Example?
-
Hi
Ran your code.
You dont have any items ?
I dont see you add anything to the list and it also reports zeroAh you do add one here
void MyModel::fetchRootDirectory() { _items.push_back(new MyItem("first_item")); }
so the rowCount code seems not correct.
-
-
@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 :)