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.
Forum Updated to NodeBB v4.3 + New Features

Qt Model/View inserting data.

Scheduled Pinned Locked Moved Unsolved General and Desktop
7 Posts 3 Posters 1.0k 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.
  • B Offline
    B Offline
    BranislavM
    wrote on 6 Jul 2018, 10:30 last edited by BranislavM 7 Jun 2018, 10:48
    #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

    R 1 Reply Last reply 6 Jul 2018, 10:41
    0
    • V Offline
      V Offline
      VRonin
      wrote on 6 Jul 2018, 10:35 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 6 Jul 2018, 10:46
      2
      • B BranislavM
        6 Jul 2018, 10:30

        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

        R Offline
        R Offline
        raven-worx
        Moderators
        wrote on 6 Jul 2018, 10:41 last edited by raven-worx 7 Jun 2018, 10:42
        #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
        • V VRonin
          6 Jul 2018, 10:35

          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 6 Jul 2018, 10:46 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.

          R 1 Reply Last reply 6 Jul 2018, 10:49
          0
          • V Offline
            V Offline
            VRonin
            wrote on 6 Jul 2018, 10:48 last edited by VRonin 7 Jun 2018, 10:49
            #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
              6 Jul 2018, 10:46

              @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.

              R Offline
              R Offline
              raven-worx
              Moderators
              wrote on 6 Jul 2018, 10:49 last edited by raven-worx 7 Jun 2018, 10:50
              #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 6 Jul 2018, 11:09 last edited by BranislavM 7 Jun 2018, 11:09
                #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

                6/7

                6 Jul 2018, 10:49

                • Login

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