Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Qt for Python
  4. iterate through treeview (QAbstractItemModel) of an older topic (see link)
Forum Updated to NodeBB v4.3 + New Features

iterate through treeview (QAbstractItemModel) of an older topic (see link)

Scheduled Pinned Locked Moved Unsolved Qt for Python
8 Posts 4 Posters 1.5k Views 1 Watching
  • 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.
  • M Offline
    M Offline
    MaMo
    wrote on last edited by MaMo
    #1

    Re: https://forum.qt.io/post/647207

    The code above in an older topic discribes nearly what I need. But now my question to it: how can I iterate over all items of the tree when it has been created?

    e.g. to write data of the tree to a csv file

    JonBJ 1 Reply Last reply
    0
    • M MaMo

      Re: https://forum.qt.io/post/647207

      The code above in an older topic discribes nearly what I need. But now my question to it: how can I iterate over all items of the tree when it has been created?

      e.g. to write data of the tree to a csv file

      JonBJ Offline
      JonBJ Offline
      JonB
      wrote on last edited by JonB
      #2

      @MaMo
      You should (probably) write a recursive function to descend the tree depth-first, taking into account that tree models have indexes which require you to pass the correct parent. have a read of https://doc.qt.io/qt-6/qmodelindex.html#details and https://doc.qt.io/qt-6/model-view-programming.html#navigation-and-model-index-creation.

      You might be able to get a list of all the indexes via https://doc.qt.io/qt-6/qabstractitemmodel.html#match with something like:

      QModelIndexList matches =  model->match(rootIndex, Qt::DisplayRole, "", -1, Qt::MatchFlags(Qt::MatchStartsWith | Qt::MatchRecursive));
      

      [Just seen you are Python, so Python equivalent.] But this pre-generates the entire list, and per the docs we are not sure what order it comes out in. I prefer the self-coded recursive approach.

      Be aware that writing to a CSV file will produce a flat output, i.e. you will not know where items are in the tree hierarchy, two adjacent items might be siblings, parent->child, or child->parent. If you don't care about that it's fine, if you do care you will need to output some extra column into the CSV to indicate parent/childhood.

      1 Reply Last reply
      0
      • SGaistS Offline
        SGaistS Offline
        SGaist
        Lifetime Qt Champion
        wrote on last edited by
        #3

        Hi,

        I concur with @JonB, a CSV file does not fit a tree view data structure. JSON or XML would be a better choice.

        Interested in AI ? www.idiap.ch
        Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

        1 Reply Last reply
        0
        • M Offline
          M Offline
          MaMo
          wrote on last edited by
          #4

          Csv was just an example. whether i export it as csv or xml doesn't matter for now. My problem at the moment is to get all the data from the treeview, e.g. after clicking on a button → unfortunately i can't do that.

          JonBJ 1 Reply Last reply
          0
          • M MaMo

            Csv was just an example. whether i export it as csv or xml doesn't matter for now. My problem at the moment is to get all the data from the treeview, e.g. after clicking on a button → unfortunately i can't do that.

            JonBJ Offline
            JonBJ Offline
            JonB
            wrote on last edited by
            #5

            @MaMo said in iterate through treeview (QAbstractItemModel) of an older topic (see link):

            unfortunately i can't do that.

            I suspect you mean, or should mean, from the model rather than from the QTreeView. Presumably all the information you want is in the model; QTreeView is a QWidget (and complex at that), I don;t think you will want to extract its information.

            So I suggested above how to go about getting the information from the model. You just need to write the code for what you want.

            M 1 Reply Last reply
            1
            • JonBJ JonB

              @MaMo said in iterate through treeview (QAbstractItemModel) of an older topic (see link):

              unfortunately i can't do that.

              I suspect you mean, or should mean, from the model rather than from the QTreeView. Presumably all the information you want is in the model; QTreeView is a QWidget (and complex at that), I don;t think you will want to extract its information.

              So I suggested above how to go about getting the information from the model. You just need to write the code for what you want.

              M Offline
              M Offline
              MaMo
              wrote on last edited by
              #6

              @JonB
              And how exactly do i get the model for the example above when I click on a button in Python??

              jsulmJ JonBJ 2 Replies Last reply
              0
              • M MaMo

                @JonB
                And how exactly do i get the model for the example above when I click on a button in Python??

                jsulmJ Offline
                jsulmJ Offline
                jsulm
                Lifetime Qt Champion
                wrote on last edited by
                #7

                @MaMo said in iterate through treeview (QAbstractItemModel) of an older topic (see link):

                And how exactly do i get the model for the example above when I click on a button in Python??

                Well, you're creating the model, right? So, just store it as member variable.

                https://forum.qt.io/topic/113070/qt-code-of-conduct

                1 Reply Last reply
                1
                • M MaMo

                  @JonB
                  And how exactly do i get the model for the example above when I click on a button in Python??

                  JonBJ Offline
                  JonBJ Offline
                  JonB
                  wrote on last edited by
                  #8

                  @MaMo
                  As @jsulm says, you created the model, right? So you can keep it in a member variable of whatever UI class you are in, perhaps where the QTreeView is, perhaps it's your QMainWindow or something. Or, wherever it is, you set it via QTreeView.setModel() so you can always retrieve it via QTreeView.model().

                  1 Reply Last reply
                  1

                  • Login

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