qTreeView with custom model not working.
-
i read the documentation and i understand the meaning of beginInsertRows() and endInsertRows(). but i dont think its the problem. indeed if you check TreeModel.cpp the headerData method return just a test element but the treeview dont even show header informations.
-
@superdu Your code has several problems like the one I already pointed out to you. The other problem that you indicate is that the columnCount (which is the same number of headers) depends on the rootItem,
rootItem->columnCount()
is 0 so the number of columns(and headers) is 0. Make thecolumnCount()
ofrootItem
at least 1 and you will see that the header will appear -
ok so i tried what you said to just return 1 at the columnCount but nothing happen. For your first comment, its already implemented in TreeModel::insertRows so i could use this method to add rows than directly add child to the root but i dont know how to use QModelIndex. What should i store in?
-
In both
rowCount()
andcolumnCount()
you have:if (!parent.isValid()) return 0;
The top level item has an invalid parent so your model will always have 0 rows and 0 columns.
P.S.
When implementing a new model, always run it through the model test it's an invaluable resource for spotting mistakes -
Thank you for your help and your tips. i deleted this if statements from this methodes but that still not working. I already tried to use QAbstractItemModelTester but that doesn't return any problems. maybe im not using it in the good way. I also tried with the three FailureReportingMode but notihng. its like if the model is not connected to the view at all but i dont understand why. i put some qdebug in the model's methods but they are no any outputs of them from the header method.
-
thank you for your advice but it's more or less what i did already ^^. the problem is the fact that for the official example they read text file which is different from what i'm looking for. But if you check the example you will see that my classes have same names and definitions of methodes are praticly the same than the official example. maybe you have other examples which can help me?
-
If you want another example you can have a look at this implementation but I fear it’s a bit complicated for your stage.
Can I ask why you decided to go down the hell of making a model instead of using a ready-made one (like QStandardItemModel)? -
yep, i tried standardmodel and that works but my items can store more things than strings so i did a class which inherits standard item class. after that i dont remember well why but i didn't arrived to do what i want with standard model because of type error. I foud a potential solution on internet but it was not elegant. So i thought about developping my own model for learn and for have the possibility to complexify the widget in the future . thank you for your example by the way