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. Qt Model/View inserting data.
QtWS25 Last Chance

Qt Model/View inserting data.

Scheduled Pinned Locked Moved Unsolved General and Desktop
7 Posts 3 Posters 1.0k 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.
  • B Offline
    B Offline
    BranislavM
    wrote on last edited by BranislavM
    #1

    Hi,
    I'm implementing Qt Model/View for tree model. I've TreeModel derived from QAbstractItemModel and have TreeItem for underlying model.
    My question is how to insert data to my model. Should I use 1) TreeModel::insertRows with (beginInsertRows/endInsertRows) or 2) insert data directly to TreeItem.
    If I use 2) how to notify view about insertion, and if I use 1) how to insert data in bulk , will every beginInsertRows/endInsertRows calls hit performance?

    Thank you.

    Edit: TreeModel::insertRow -> TreeModel::insertRows

    raven-worxR 1 Reply Last reply
    0
    • VRoninV Offline
      VRoninV Offline
      VRonin
      wrote on last edited by
      #2

      Use 1) but instead of reimplementing TreeModel::insertRow reimplement TreeModel::insertRows so you can add data in bulk

      "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
      ~Napoleon Bonaparte

      On a crusade to banish setIndexWidget() from the holy land of Qt

      B 1 Reply Last reply
      2
      • B BranislavM

        Hi,
        I'm implementing Qt Model/View for tree model. I've TreeModel derived from QAbstractItemModel and have TreeItem for underlying model.
        My question is how to insert data to my model. Should I use 1) TreeModel::insertRows with (beginInsertRows/endInsertRows) or 2) insert data directly to TreeItem.
        If I use 2) how to notify view about insertion, and if I use 1) how to insert data in bulk , will every beginInsertRows/endInsertRows calls hit performance?

        Thank you.

        Edit: TreeModel::insertRow -> TreeModel::insertRows

        raven-worxR Offline
        raven-worxR Offline
        raven-worx
        Moderators
        wrote on last edited by raven-worx
        #3

        @BranislavM
        to add up to @VRonin:
        insertRow implicitly calls insertRows with the according parameters.
        As a general principle, only virtual methods are meant to be overridden.

        --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
        If you have a question please use the forum so others can benefit from the solution in the future

        1 Reply Last reply
        3
        • VRoninV VRonin

          Use 1) but instead of reimplementing TreeModel::insertRow reimplement TreeModel::insertRows so you can add data in bulk

          B Offline
          B Offline
          BranislavM
          wrote on last edited by
          #4

          @VRonin
          Thank you for replay. What about if I have to add data in bulk, but its not always jost rows, but rows with children. The thing is I cant
          just create root node for row with children, and add it, becouse I'm creating node hierarchy (file/folder structure) on the fly, so if I already have node for folder, I'm just adding child node, otherwise I must create folder node.

          I suppose there is no straight way, but must do optimisation for myself? So 1) is prefered way.

          raven-worxR 1 Reply Last reply
          0
          • VRoninV Offline
            VRoninV Offline
            VRonin
            wrote on last edited by VRonin
            #5

            If you use 1) and the various calls to beginInsertRows/endInsertRows are done in sequence without the control going back to the event loop, the view should optimise the repaint automatically

            "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
            ~Napoleon Bonaparte

            On a crusade to banish setIndexWidget() from the holy land of Qt

            1 Reply Last reply
            0
            • B BranislavM

              @VRonin
              Thank you for replay. What about if I have to add data in bulk, but its not always jost rows, but rows with children. The thing is I cant
              just create root node for row with children, and add it, becouse I'm creating node hierarchy (file/folder structure) on the fly, so if I already have node for folder, I'm just adding child node, otherwise I must create folder node.

              I suppose there is no straight way, but must do optimisation for myself? So 1) is prefered way.

              raven-worxR Offline
              raven-worxR Offline
              raven-worx
              Moderators
              wrote on last edited by raven-worx
              #6

              @BranislavM said in Qt Model/View inserting data.:

              Thank you for replay. What about if I have to add data in bulk, but its not always jost rows, but rows with children.

              then (2) is probably better.

              void addDataWithChildren()
              {
                  this->beginInsertRows(...);
              
                  // add data to your internal data structure with children, the view then will ask for the childCount of the inserted row when it needs to
              
                  this->endInsertRows();
              }
              

              --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
              If you have a question please use the forum so others can benefit from the solution in the future

              1 Reply Last reply
              0
              • B Offline
                B Offline
                BranislavM
                wrote on last edited by BranislavM
                #7

                I started with (2) approach as @raven-worx supposed, and it's good for initialization. But I wanted to use the same code between beginInsertRows/endInsertRows calls for adding files, and problem is sometimes nodes already exist, and sometimes I need to add missing nodes, to make path structure. So its not always the same parent node where rows are inserted.
                I suppose I need to use combination of (1) and (2).

                1 Reply Last reply
                0

                • Login

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