PDA

View Full Version : Custom Delegate for QTableView in QCompleter not invoked



flautzr
23rd June 2020, 16:06
Hello,

i have a QLineEdit with a QCompleter which is used as a search bar. The customer now wants an icon to be displayed at the end of each completer row, but only when the entry is highlighted or when the mouse is hovering over it.
I tried to achieve this by overriding the paint method of a custom delegate derived from QStyledItemDelegate, as I have done it before with a QTreeView.

I basically duplicated the procedure for installing the delegate but the overridden methods dont get invoked at all.



ptrModel = new QStandardItemModel();

ptrView = new QTableView();
ptrDelegate = new SearchBarDelegate();
ptrView->verticalHeader()->setVisible(false);
ptrView->horizontalHeader()->setVisible(false);
ptrView->setSelectionBehavior(QTableView::SelectRows);
ptrView->setShowGrid(false);

ptrView->setItemDelegate(ptrDelegate);

ptrCompleter = new QCompleter(ptrModel, this);
ptrCompleter->setCompletionColumn(0);
ptrCompleter->setCompletionMode(QCompleter::UnfilteredPopupCompl etion);
ptrCompleter->setCaseSensitivity(Qt::CaseInsensitive);
ptrCompleter->setMaxVisibleItems(amountSearchRecommendations);

ptrCompleter->setPopup(ptrView);

setCompleter(ptrCompleter);


and the delegate class looks like this



class SearchBarDelegate : public QStyledItemDelegate
{
Q_OBJECT

public:
SearchBarDelegate(QWidget *parent = Q_NULLPTR) :
QItemDelegate(parent)
{ }

void paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const Q_DECL_OVERRIDE;
QSize sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const Q_DECL_OVERRIDE;
};


I tried to subclass it from QItemDelegate, QAbstractItemDelegate but to no avail.

thx in advance