Total count of items in a tree view



  • Is there a specific qt function that will return the number of items in a tree view?


  • Moderators

    No, as this would be potentially very slow operation (imagine something like a filesystem model).
    If needed it should be implemented as a method of your customized model.



  • What you could do (which I have done in the past Qt4.x or Qt5.x) is create a QList< > or QMap<> object in your main Class that manages the TreeView Objects. Then when you read in and populate the QTree via XML file load, direct addition editing the QTree view, etc then simultaneously add the new item (s) (or just an index count) to the QList or QMap that manages the QTreeView. This allows you to use the ->count() method of the QList or QMap to keep track of the total number of items(added, deleted, etc). Of course, if you get into Parent or Child issues (nested items) you will have to devise a way to keep those Parent or Child items also updated appropriately for the proper count you are looking for.

    Hope that helps!

    -Vince


  • Moderators

    As I said this method belongs in the model. Any model usually has some way of adding/removing data e.g. addStuff(Stuff stuff) and removeStuff(SomeID id), be it from xml, database, network, filesystem or manually.
    There's no need to copy all the data into a list or tree just to get items count. All you need is a single counter increased/decreased in these methods and returned via a method like count().



  • Good point Chris. The reason why I suggested otherwise is one can often store data items in QLists or QMaps that might also be needed besides a simple item counter when associated with QTreeView. But I agree, extending the associated 'model' as you suggested is much more appropriate.

    -Vince


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.