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.
ptrDelegate = new SearchBarDelegate();
ptrView->verticalHeader()->setVisible(false);
ptrView->horizontalHeader()->setVisible(false);
ptrView
->setSelectionBehavior
(QTableView::SelectRows);
ptrView->setShowGrid(false);
ptrView->setItemDelegate(ptrDelegate);
ptrCompleter->setCompletionColumn(0);
ptrCompleter
->setCompletionMode
(QCompleter::UnfilteredPopupCompletion);
ptrCompleter->setCaseSensitivity(Qt::CaseInsensitive);
ptrCompleter->setMaxVisibleItems(amountSearchRecommendations);
ptrCompleter->setPopup(ptrView);
setCompleter(ptrCompleter);
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::UnfilteredPopupCompletion);
ptrCompleter->setCaseSensitivity(Qt::CaseInsensitive);
ptrCompleter->setMaxVisibleItems(amountSearchRecommendations);
ptrCompleter->setPopup(ptrView);
setCompleter(ptrCompleter);
To copy to clipboard, switch view to plain text mode
and the delegate class looks like this
class SearchBarDelegate : public QStyledItemDelegate
{
Q_OBJECT
public:
SearchBarDelegate
(QWidget *parent
= Q_NULLPTR
) : { }
};
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;
};
To copy to clipboard, switch view to plain text mode
I tried to subclass it from QItemDelegate, QAbstractItemDelegate but to no avail.
thx in advance
Bookmarks