Solved Problem while applying filter on QTableView with a button column
-
@Lati
I don't understand. The view is "filled again" when the filter changes, so the push buttons need to be created then.Separately, I don't know if
setIndexWidget()
is the right thing to do. https://doc.qt.io/qt-5/qabstractitemview.html#setIndexWidgetThis function should only be used to display static content within the visible area corresponding to an item of data. If you want to display custom dynamic content or implement a custom editor widget, subclass QStyledItemDelegate instead.
-
@JonB It seems then the issue is related to the
setIndexWidget()
. I missed that setIndexWidget should only be used for static content and thank you for pointing this. I will update the code withQStyledItemDelegate
instead and see if it works.Thanks again.
-
-
-
Who summoned my whrath?!
In this case you have to fill a
QStyleOptionButton
in the delegate'spaint
method and simply react to theclicked
event of the view. It's just a bit tedious to type in the code but it's not complicated -
@VRonin Ask for your forgiveness your highness! :)
Thank you for your suggestion, can you please show me an example code or a sample project?
-
Something like this
class ButtonDelegate : public QStyledItemDelegate { Q_OBJECT Q_DISABLE_COPY(ButtonDelegate) private: QStyleOptionButton prepareButtonOption(const QStyleOptionViewItem &option) const{ QStyleOptionButton buttonOption; buttonOption.text = tr("Add Interval"); buttonOption.rect = option.rect buttonOption.features = QStyleOptionButton::None; buttonOption.direction = option.direction; buttonOption.fontMetrics = option.fontMetrics; buttonOption.palette = option.palette; buttonOption.styleObject = option.styleObject; return buttonOption; } public: explicit ButtonDelegate(QObject* parent = Q_NULLPTR) :QStyledItemDelegate(parent) {} void paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const Q_DECL_OVERRIDE{ Q_ASSERT(index.isValid()); QStyleOptionViewItem opt = option; initStyleOption(&opt, index); const QWidget *widget = option.widget; QStyle *style = widget ? widget->style() : QApplication::style(); QStyleOptionButton btnOption=prepareButtonOption(opt); style->drawControl(QStyle::CE_PushButton, &btnOption, painter, widget); } QSize sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const Q_DECL_OVERRIDE{ QStyleOptionButton btnOpt=prepareButtonOption(opt); const QSize fntSize = fontMetrics().size(Qt::TextShowMnemonic,btnOpt.text); const QWidget *widget = option.widget; QStyle *style = widget ? widget->style() : QApplication::style(); return style->sizeFromContents(CT_PushButton,&btnOpt,fntSize,this); } }
-
@VRonin
That is indeed a lot of code compared against the originalsetIndexWidget(item, new QPushButton("..."));
! Makes me want to stick with that approach ;-) -
@JonB said in Problem while applying filter on QTableView with a button column:
Makes me want to stick with that approach
Teasing eh?! Cheeky sod!
-
@VRonin
I'm not the one who is teasing! When I discovered them I was quite happy withsetIndexWidget()
and whatever other methods which let you set a widget in a table etc. Simple, easy to use. Then you came along and put the fear of God into me & anyone else who chooses to use this approach, saying we would run out of memory and be damned for eternity, be kicked out of the EU, etc. ;-) So now I wouldn't dare use them.... -
@VRonin Thanks for the code! I will try to implement after few modifications.
-
@JonB Absolutely agree and disagree :) Wish filtering would work with
setIndexWidget
and I wouldn't loose time to implement the same thing with 100x more codes
But for static tables, I will usesetIndexWidget
:) -
@VRonin It is implemented, working good. However, I have an issue. When
setItemDelegateForColumn
is used, all created buttons at the column have the same properties (the last row button's properties). How will I know the row number of the button I clicked?I create the buttons as following:
ButtonDelegateTableView * addIntervalButton = new ButtonDelegateTableView(ui->tableView, project, wellName, rowNumber);
And, when I clicked on the button at the first row, I get the properties (well name, row number etc.) of the last row buttons.