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. How to set data for the new row when override QAbstractItemModel::insertRows

How to set data for the new row when override QAbstractItemModel::insertRows

Scheduled Pinned Locked Moved Solved General and Desktop
10 Posts 4 Posters 2.3k 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.
  • jronaldJ Offline
    jronaldJ Offline
    jronald
    wrote on last edited by
    #1

    override virtual QAbstractItemModel::insertRows(int row, int count, const QModelIndex &parent = QModelIndex()), no data in parameters, how to set data for the new row?

    raven-worxR 1 Reply Last reply
    0
    • jronaldJ jronald

      override virtual QAbstractItemModel::insertRows(int row, int count, const QModelIndex &parent = QModelIndex()), no data in parameters, how to set data for the new row?

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

      @jronald
      call setData() after the row(s) have been successfully inserted

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

      jronaldJ VRoninV 2 Replies Last reply
      3
      • raven-worxR raven-worx

        @jronald
        call setData() after the row(s) have been successfully inserted

        jronaldJ Offline
        jronaldJ Offline
        jronald
        wrote on last edited by
        #3

        @raven-worx said in How to set data for the new row when override QAbstractItemModel::insertRows:

        @jronald
        call setData() after the row(s) have been successfully inserted

        Is this the formal way?
        Thanks

        D 1 Reply Last reply
        0
        • jronaldJ jronald

          @raven-worx said in How to set data for the new row when override QAbstractItemModel::insertRows:

          @jronald
          call setData() after the row(s) have been successfully inserted

          Is this the formal way?
          Thanks

          D Offline
          D Offline
          deleted372
          wrote on last edited by VRonin
          #4

          @jronald
          It depends what you have as model. For example, if you store your data in QList<myType>myList you can do like this:

          bool myModel::insertRows(int position, int rows, const QModelIndex &parentIndex)
          {
          Q_ASSERT(checkIndex(parentIndex));
          if(position<0 || position>rowCount(parentIndex) || rows<=0)
          return false;
             beginInsertRows(parentIndex, position, position+rows-1);
             while (rows--) 
                myList.insert(position, new myType("arg1", "arg2", "arg3"));
            endInsertRows();
            return true;
          }
          

          [VRonin] edited indexes in beginInsertRows to be correct and added input checks

          raven-worxR jronaldJ 2 Replies Last reply
          0
          • D deleted372

            @jronald
            It depends what you have as model. For example, if you store your data in QList<myType>myList you can do like this:

            bool myModel::insertRows(int position, int rows, const QModelIndex &parentIndex)
            {
            Q_ASSERT(checkIndex(parentIndex));
            if(position<0 || position>rowCount(parentIndex) || rows<=0)
            return false;
               beginInsertRows(parentIndex, position, position+rows-1);
               while (rows--) 
                  myList.insert(position, new myType("arg1", "arg2", "arg3"));
              endInsertRows();
              return true;
            }
            

            [VRonin] edited indexes in beginInsertRows to be correct and added input checks

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

            @JoZCaVaLLo
            this wont work.
            without the necessary beginInsertRows()/endInsertRows() calls the view wont update.
            Also in your code where are the arg variables coming from?

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

            D 1 Reply Last reply
            3
            • D deleted372

              @jronald
              It depends what you have as model. For example, if you store your data in QList<myType>myList you can do like this:

              bool myModel::insertRows(int position, int rows, const QModelIndex &parentIndex)
              {
              Q_ASSERT(checkIndex(parentIndex));
              if(position<0 || position>rowCount(parentIndex) || rows<=0)
              return false;
                 beginInsertRows(parentIndex, position, position+rows-1);
                 while (rows--) 
                    myList.insert(position, new myType("arg1", "arg2", "arg3"));
                endInsertRows();
                return true;
              }
              

              [VRonin] edited indexes in beginInsertRows to be correct and added input checks

              jronaldJ Offline
              jronaldJ Offline
              jronald
              wrote on last edited by
              #6

              @JoZCaVaLLo said in How to set data for the new row when override QAbstractItemModel::insertRows:

              @jronald
              It depends what you have as model. For example, if you store your data in QList<myType>myList you can do like this:

                    myList.insert(position, new myType(arg1, arg2, arg3));
              

              Where do arg1 arg2 arg3 come from?

              D 1 Reply Last reply
              0
              • raven-worxR raven-worx

                @JoZCaVaLLo
                this wont work.
                without the necessary beginInsertRows()/endInsertRows() calls the view wont update.
                Also in your code where are the arg variables coming from?

                D Offline
                D Offline
                deleted372
                wrote on last edited by
                #7

                @raven-worx
                You are right... I edited the code!

                1 Reply Last reply
                0
                • jronaldJ jronald

                  @JoZCaVaLLo said in How to set data for the new row when override QAbstractItemModel::insertRows:

                  @jronald
                  It depends what you have as model. For example, if you store your data in QList<myType>myList you can do like this:

                        myList.insert(position, new myType(arg1, arg2, arg3));
                  

                  Where do arg1 arg2 arg3 come from?

                  D Offline
                  D Offline
                  deleted372
                  wrote on last edited by
                  #8

                  @jronald
                  it's an example... the model depends up to you... arg1 arg2 arg3 can be anything...

                  jronaldJ 1 Reply Last reply
                  0
                  • D deleted372

                    @jronald
                    it's an example... the model depends up to you... arg1 arg2 arg3 can be anything...

                    jronaldJ Offline
                    jronaldJ Offline
                    jronald
                    wrote on last edited by
                    #9

                    @JoZCaVaLLo said in How to set data for the new row when override QAbstractItemModel::insertRows:

                    @jronald
                    it's an example... the model depends up to you... arg1 arg2 arg3 can be anything...

                    so arg1 arg2 arg3 are member variables?

                    1 Reply Last reply
                    0
                    • raven-worxR raven-worx

                      @jronald
                      call setData() after the row(s) have been successfully inserted

                      VRoninV Offline
                      VRoninV Offline
                      VRonin
                      wrote on last edited by
                      #10

                      To clarify:
                      @raven-worx's answer is the only way sticking only to the QAbtsractItemModel interface.

                      call setData() after the row(s) have been successfully inserted

                      You are free, of course, to implement your own custom interface that allows for insertion with data either by creating something like bool insertRowData(int row, const QVector<QVariant>& dataColumns); or by having a constant or a member variable define what the default data should be

                      "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
                      3

                      • Login

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