Qt Forum

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

    Unsolved Qt Model/View inserting data.

    General and Desktop
    3
    7
    423
    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.
    • B
      BranislavM last edited by 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-worx 1 Reply Last reply Reply Quote 0
      • VRonin
        VRonin last edited by

        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 Reply Quote 2
        • raven-worx
          raven-worx Moderators @BranislavM last edited by raven-worx

          @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 Reply Quote 3
          • B
            BranislavM @VRonin last edited by

            @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-worx 1 Reply Last reply Reply Quote 0
            • VRonin
              VRonin last edited by VRonin

              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 Reply Quote 0
              • raven-worx
                raven-worx Moderators @BranislavM last edited by raven-worx

                @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 Reply Quote 0
                • B
                  BranislavM last edited by BranislavM

                  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 Reply Quote 0
                  • First post
                    Last post