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 add a row to a subclass of QAbstractTableModel?
QtWS25 Last Chance

How to add a row to a subclass of QAbstractTableModel?

Scheduled Pinned Locked Moved Unsolved General and Desktop
table viewtable model
5 Posts 3 Posters 439 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.
  • J Offline
    J Offline
    jdent
    wrote on 25 Jul 2024, 23:48 last edited by jdent
    #1

    I have a subclass of QAbstractTableModel which internally manages a vector of tuples.
    I need to insert a new row at a certain row number. How can this be done?

    Can I just insert a tuple into the vector and ask the model to redisplay but not loosing the current row?
    How do I find out which is the current row number in the QTableView?

    Regards,
    Juan

    P J 2 Replies Last reply 26 Jul 2024, 02:17
    0
    • J jdent
      25 Jul 2024, 23:48

      I have a subclass of QAbstractTableModel which internally manages a vector of tuples.
      I need to insert a new row at a certain row number. How can this be done?

      Can I just insert a tuple into the vector and ask the model to redisplay but not loosing the current row?
      How do I find out which is the current row number in the QTableView?

      Regards,
      Juan

      P Offline
      P Offline
      Pl45m4
      wrote on 26 Jul 2024, 02:17 last edited by
      #2

      @jdent said in How to add a row to a subclass of QAbstractTableModel?:

      I need to insert a new row at a certain row number. How can this be done?

      Re-implement QAbstractTableModel::insertRows

      • https://doc.qt.io/qt-6/qabstractitemmodel.html#insertRows

      Can I just insert a tuple into the vector and ask the model to redisplay but not loosing the current row?

      It might work, but is a bad idea.

      How do I find out which is the current row number in the QTableView?

      rowCount(), also re-implement it in your model, so it always returns the correct number of rows.

      • https://doc.qt.io/qt-6/qabstractitemmodel.html#rowCount

      For more detail check out the Editable TreeView Example
      (it's TreeView but in general the insert/remove procedure of rows and cols is the same)

      • https://doc.qt.io/qt-6/qtwidgets-itemviews-editabletreemodel-example.html

      If debugging is the process of removing software bugs, then programming must be the process of putting them in.

      ~E. W. Dijkstra

      J P 2 Replies Last reply 27 Jul 2024, 15:48
      2
      • J jdent
        25 Jul 2024, 23:48

        I have a subclass of QAbstractTableModel which internally manages a vector of tuples.
        I need to insert a new row at a certain row number. How can this be done?

        Can I just insert a tuple into the vector and ask the model to redisplay but not loosing the current row?
        How do I find out which is the current row number in the QTableView?

        Regards,
        Juan

        J Offline
        J Offline
        JonB
        wrote on 26 Jul 2024, 07:01 last edited by JonB
        #3

        @jdent said in How to add a row to a subclass of QAbstractTableModel?:

        How do I find out which is the current row number in the QTableView?

        Depending on what you want, any of the following:

        • QModelIndex QAbstractItemView::currentIndex() const
        • QModelIndexList QTableView::selectedIndexes() const
        • QItemSelectionModel *QAbstractItemView::selectionModel() const and call QModelIndex QItemSelectionModel::currentIndex() const

        However, note that usually you do not need the currently selected row number. If you are asking about this in order to preserve/restore the current row after making a change: if you insert/delete/update a row I think you will find the table view maintains the currently selected row for you.

        If you refill/invalidate the whole table and wish to retain/restore the previous current row you should (probably) not do that by row number as that may have changed due to to the new data. Rather you should look at the data in the current row before the refresh, keep some "primary key" which uniquely identifies the data in that row, refill the data, find the index in the new data of the saved "primary key" and call void QItemSelectionModel::select(const QModelIndex &index, QItemSelectionModel::SelectionFlags command) to reselect wherever that item appears in the model's new data.

        1 Reply Last reply
        0
        • P Pl45m4
          26 Jul 2024, 02:17

          @jdent said in How to add a row to a subclass of QAbstractTableModel?:

          I need to insert a new row at a certain row number. How can this be done?

          Re-implement QAbstractTableModel::insertRows

          • https://doc.qt.io/qt-6/qabstractitemmodel.html#insertRows

          Can I just insert a tuple into the vector and ask the model to redisplay but not loosing the current row?

          It might work, but is a bad idea.

          How do I find out which is the current row number in the QTableView?

          rowCount(), also re-implement it in your model, so it always returns the correct number of rows.

          • https://doc.qt.io/qt-6/qabstractitemmodel.html#rowCount

          For more detail check out the Editable TreeView Example
          (it's TreeView but in general the insert/remove procedure of rows and cols is the same)

          • https://doc.qt.io/qt-6/qtwidgets-itemviews-editabletreemodel-example.html
          J Offline
          J Offline
          jdent
          wrote on 27 Jul 2024, 15:48 last edited by
          #4

          @Pl45m4 Is there an example for implementing insertRows?

          1 Reply Last reply
          0
          • P Pl45m4
            26 Jul 2024, 02:17

            @jdent said in How to add a row to a subclass of QAbstractTableModel?:

            I need to insert a new row at a certain row number. How can this be done?

            Re-implement QAbstractTableModel::insertRows

            • https://doc.qt.io/qt-6/qabstractitemmodel.html#insertRows

            Can I just insert a tuple into the vector and ask the model to redisplay but not loosing the current row?

            It might work, but is a bad idea.

            How do I find out which is the current row number in the QTableView?

            rowCount(), also re-implement it in your model, so it always returns the correct number of rows.

            • https://doc.qt.io/qt-6/qabstractitemmodel.html#rowCount

            For more detail check out the Editable TreeView Example
            (it's TreeView but in general the insert/remove procedure of rows and cols is the same)

            • https://doc.qt.io/qt-6/qtwidgets-itemviews-editabletreemodel-example.html
            P Offline
            P Offline
            Pl45m4
            wrote on 27 Jul 2024, 16:14 last edited by
            #5

            @Pl45m4 said in How to add a row to a subclass of QAbstractTableModel?:

            For more detail check out the Editable TreeView Example
            (it's TreeView but in general the insert/remove procedure of rows and cols is the same)

            https://doc.qt.io/qt-6/qtwidgets-itemviews-editabletreemodel-example.html

            @jdent see here


            If debugging is the process of removing software bugs, then programming must be the process of putting them in.

            ~E. W. Dijkstra

            1 Reply Last reply
            1

            5/5

            27 Jul 2024, 16:14

            • Login

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