Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Total count of items in a tree view
QtWS25 Last Chance

Total count of items in a tree view

Scheduled Pinned Locked Moved General and Desktop
treecount
5 Posts 3 Posters 4.5k Views
  • 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 Offline
    J Offline
    joeydonovan4
    wrote on 2 Apr 2015, 14:55 last edited by
    #1

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

    1 Reply Last reply
    1
    • C Offline
      C Offline
      Chris Kawa
      Lifetime Qt Champion
      wrote on 2 Apr 2015, 16:02 last edited by
      #2

      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
      0
      • V Offline
        V Offline
        Vince Golubic
        wrote on 2 Apr 2015, 18:21 last edited by
        #3

        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
        0
        • C Offline
          C Offline
          Chris Kawa
          Lifetime Qt Champion
          wrote on 2 Apr 2015, 19:47 last edited by
          #4

          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
          1
          • V Offline
            V Offline
            Vince Golubic
            wrote on 2 Apr 2015, 22:06 last edited by
            #5

            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
            1

            2/5

            2 Apr 2015, 16:02

            • Login

            • Login or register to search.
            2 out of 5
            • First post
              2/5
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • Users
            • Groups
            • Search
            • Get Qt Extensions
            • Unsolved