Adding "Edit" and "Remove" columns to table
-
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:
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 thatHere 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?
-
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:
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 thatHere 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?
@keesaev
I would add a proxy model as the very last source model in the chain, just before theQTableView
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. -
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?@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. -
@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.@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?