How to prevent a QIcon from getting highlighted?
Unsolved
General and Desktop
-
I'm trying to prevent the icons set on a QComboBox to get highlighted when the mouse is currently over the item.
I tried to use QStyleItemDelegate and ignore the flag MouseOver but this approach stops the painter from painting the 'hovered' background of the item and it still draw the icon highlight.
What else i could try?
class ComboBoxDelegate : public QStyledItemDelegate { Q_OBJECT public: QComboBox* comboBox = nullptr; ComboBoxDelegate(QComboBox* comboBox) : QStyledItemDelegate(comboBox), comboBox(comboBox) {}; void paint(QPainter* painter, const QStyleOptionViewItem& option, const QModelIndex& index) const override { QStyleOptionViewItem opt = option; // Check if the current item is hovered if (opt.state & QStyle::State_MouseOver) opt.state = QStyle::State_None; return QStyledItemDelegate::paint(painter, opt, index); } }; class ComboBox : public QComboBox { public: ComboBoxDelegate* comboBoxDelegate = nullptr; ComboBox(QWidget *parent = nullptr) : QComboBox(parent) { comboBoxDelegate = new ComboBoxDelegate(this); setItemDelegate(comboBoxDelegate); } }
-
Hi,
The next step is to do the painting yourself.
-
@Bonnie add an icon to a QComboBox and move the mouse over it, the icon is highlighted/colored blue.
https://doc.qt.io/qt-6/qstyleoptionviewitem.html#showDecorationSelected-var
"This variable holds whether the decoration should be highlighted on selected items"
Even setting it false, it still highlights the icon, a bug?
class ComboBoxDelegate : public QStyledItemDelegate { Q_OBJECT public: QComboBox* comboBox = nullptr; ComboBoxDelegate(QComboBox* comboBox) : QStyledItemDelegate(comboBox), comboBox(comboBox) {}; void paint(QPainter* painter, const QStyleOptionViewItem& option, const QModelIndex& index) const override { QStyleOptionViewItem opt = option; if (opt.showDecorationSelected) opt.showDecorationSelected = false; return QStyledItemDelegate::paint(painter, opt, index); } };
-
-
QIcon icon(":/path_to_icon"); icon.addFile(":/path_to_icon", {}, QIcon::Selected); listItem->setIcon(icon);
Tested on qt 5.15 in QListWidget.