Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Total count of items in a tree view

    General and Desktop
    tree count
    3
    5
    3693
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • J
      joeydonovan4 last edited by

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

      1 Reply Last reply Reply Quote 1
      • Chris Kawa
        Chris Kawa Moderators last edited by

        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.

        1 Reply Last reply Reply Quote 0
        • V
          Vince Golubic last edited by

          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

          1 Reply Last reply Reply Quote 0
          • Chris Kawa
            Chris Kawa Moderators last edited by

            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().

            1 Reply Last reply Reply Quote 1
            • V
              Vince Golubic last edited by

              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

              1 Reply Last reply Reply Quote 1
              • First post
                Last post