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. Adding "Edit" and "Remove" columns to table

Adding "Edit" and "Remove" columns to table

Scheduled Pinned Locked Moved Solved General and Desktop
5 Posts 2 Posters 709 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.
  • K Offline
    K Offline
    keesaev
    wrote on last edited by
    #1

    Hi everyone!

    I am working on adding "Edit" and "Remove" functionality to my table. What I'm trying to do is show same table, but with "Edit" and "Remove" buttons for each row as shown on picture below:
    af9811a5-1095-4dce-ab63-d30a833d2d33-image.png
    So the problem is: I need to display some of the columns of original model (3 of them on the screenshot) and add two extra columns.
    I am using model subclassed from QAbstractTableModel, custom header derived from QHeaderView and custom delegate derived from QAbstractItemDelegate. Also several QSortFilterProxyModel on top of that

    Here I don't really know how to approach this problem, should I subclass another QAbstractTableModel and pass it a reference to original table so I could reimplement 'data' method? Or should I subclass QAbstractProxyModel and reimplement 'data' method there?

    This problem seems to be trivial, what is the good practice to solving this problem?

    JonBJ 1 Reply Last reply
    0
    • K keesaev

      Hi everyone!

      I am working on adding "Edit" and "Remove" functionality to my table. What I'm trying to do is show same table, but with "Edit" and "Remove" buttons for each row as shown on picture below:
      af9811a5-1095-4dce-ab63-d30a833d2d33-image.png
      So the problem is: I need to display some of the columns of original model (3 of them on the screenshot) and add two extra columns.
      I am using model subclassed from QAbstractTableModel, custom header derived from QHeaderView and custom delegate derived from QAbstractItemDelegate. Also several QSortFilterProxyModel on top of that

      Here I don't really know how to approach this problem, should I subclass another QAbstractTableModel and pass it a reference to original table so I could reimplement 'data' method? Or should I subclass QAbstractProxyModel and reimplement 'data' method there?

      This problem seems to be trivial, what is the good practice to solving this problem?

      JonBJ Offline
      JonBJ Offline
      JonB
      wrote on last edited by
      #2

      @keesaev
      I would add a proxy model as the very last source model in the chain, just before the QTableView attaches. This proxy just adds column(s) for the "Edit" and "Remove" buttons/links, and has a custom delegate to render them. Otherwise it's an identity proxy to the underlying source model, whatever that is.

      1 Reply Last reply
      1
      • K Offline
        K Offline
        keesaev
        wrote on last edited by
        #3

        Thanks for reply!
        QIdentityProxyModel seems to be perfect for this problem!
        Just to be clear, it is not possible for proxy model to have different column count that source model, right?

        JonBJ 1 Reply Last reply
        0
        • K keesaev

          Thanks for reply!
          QIdentityProxyModel seems to be perfect for this problem!
          Just to be clear, it is not possible for proxy model to have different column count that source model, right?

          JonBJ Offline
          JonBJ Offline
          JonB
          wrote on last edited by JonB
          #4

          @keesaev said in Adding "Edit" and "Remove" columns to table:

          QIdentityProxyModel seems to be perfect for this problem!

          Indeed. Start from that, but subclass to add your own extra functionality.

          Just to be clear, it is not possible for proxy model to have different column count that source model, right?

          On the contrary. And that is exactly what you want to do here, to add one or two columns for your "edit"/"remove" row buttons. Derive from QIdentityProxyModel, override int QIdentityProxyModel::columnCount(const QModelIndex &parent = QModelIndex()) const to add columns, use your own delegate to show the buttons you want in that/those column(s). We put this proxy last just to add those two buttons/column(s) onto whatever "real" source model it is proxying.

          K 1 Reply Last reply
          2
          • JonBJ JonB

            @keesaev said in Adding "Edit" and "Remove" columns to table:

            QIdentityProxyModel seems to be perfect for this problem!

            Indeed. Start from that, but subclass to add your own extra functionality.

            Just to be clear, it is not possible for proxy model to have different column count that source model, right?

            On the contrary. And that is exactly what you want to do here, to add one or two columns for your "edit"/"remove" row buttons. Derive from QIdentityProxyModel, override int QIdentityProxyModel::columnCount(const QModelIndex &parent = QModelIndex()) const to add columns, use your own delegate to show the buttons you want in that/those column(s). We put this proxy last just to add those two buttons/column(s) onto whatever "real" source model it is proxying.

            K Offline
            K Offline
            keesaev
            wrote on last edited by
            #5

            @JonB
            That seems to work, thanks!

            override int QIdentityProxyModel::columnCount(const QModelIndex &parent = QModelIndex()) const

            Right now I'm struggling with displaying buttons.

            Seems like I won't be able to use QApplication::style()->drawControl for displaying a button with sophisticated styles such as rounded corners, specific font and colors.

            Am I using the right approach: displaying button with simple primitives such as drawRoundedRect and drawText, and using states such as QStyle::State_MouseOver for implementing default button behavior (color and cursor change on hover)? Or is there a simpler way?

            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