QComboBox: Disabling mouse hover highlighting
-
When my QComboBox is in drop down state it highlights the previously selected item with a blue background. However, when I hover over the QComboBox with my mouse the highlight follows the mouse hover and no longer has the previously selected item highlighted.
This is my QComboBox Setup:
QStringList strList; QComboBox* m_pComboBox = new QComboBox(this); QStringListModel* m_pModel = new QStringListModel(strList, this); QSortFilterProxyModel* m_pSortFilterProxyModel = new QSortFilterProxyModel(this); m_pSortFilterProxyModel->setSourceModel(m_pModel); m_pComboBox->setModel(m_pSortFilterProxyModel);
After doing some research I found I could subclass QStyledItemDelegate and reimplement the paint function. What would I add to achieve the behavior I want.
Original paint method for QStyledItemDelegate:
void QStyledItemDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const { Q_ASSERT(index.isValid()); QStyleOptionViewItem opt = option; initStyleOption(&opt, index); const QWidget *widget = QStyledItemDelegatePrivate::widget(option); QStyle *style = widget ? widget->style() : QApplication::style(); style->drawControl(QStyle::CE_ItemViewItem, &opt, painter, widget); }
How can I disable this and keep the highlight on the previously selected item?
-
@anshah said in QComboBox: Disabling mouse hover highlighting:
when I hover over the QComboBox with my mouse the highlight follows the mouse hover
That's how ComboBoxes work.
How do you want to select an item, if you disable this functionality? Or is it just about the highlight color?Edit:If it's just the highlight color and not the highlight behavior as such, you can use style sheet to disable the highlight color or set any color you want.~~ // Try 'none' or 'transparent' instead or use same color as comboBox BG QComboBox QAbstractItemView { selection-background-color: lightgray; } ~~
Never mind, didn't read your actual problem :)
-
@Pl45m4 Thank you for the reply.
My main objective is to indicate which item was previously selected. If highlight doesn't work I could also change the style (color, bold, font) of the selected item.For simplicity let's say I wanted to make the selected item green. How would I do that? I've tried the style suggestions for QComboBox in multiple forums but none of them seem to work.
Any help would be most appreciate.
-
@anshah said in QComboBox: Disabling mouse hover highlighting:
For simplicity let's say I wanted to make the selected item green
So you want the currently selected item to be green, no matter where the mouse cursor is?! Should it be green inside the dropdown of the comboBox or just inside the selection?
What you definitely could do, is giving your selected item a check icon. Then the user can see which item is currently in use, when using the drop down menu.
-
I did it with stylesheet once, but I cant find my code. And I also dont know if it is intended to be possible, because I cant find anything in the docs, that says, that it is possible. It was a combination of different stylesheets
https://doc.qt.io/qt-5/stylesheet-examples.html#customizing-qcombobox
I played around with different combinations and suddenly an icon (small check) appeared left to the currently active item (without setting any item icon)
QComboBox QAbstractItemView { selection-background-color: /* COLOR */ ; } QComboBox QAbstractItemView::item { // some color } QComboBox:item:selected { // color } QComboBox QAbstractItemView:active { // color }
I dont know if it was a single stylesheet or the combination of them, which leads to this behavior.
When I've found my code again, I will post it here.
-
@anshah said in QComboBox: Disabling mouse hover highlighting:
If highlight doesn't work I could also change the style (color, bold, font) of the selected item.
I think this is the quickest way. If your combobox uses the default model (i.e. you didn't call
QComboBox::setModel
) you canQObject::connect(combobox,QOverload<int>::of(&QComboBox::currentIndexChanged),combobox,[=](int idx)->void{ for(int i=0, maxI = combobox->count();i<maxI;++i){ if(i==idx) combobox->setItemData(i,QBrush(Qt::green),Qt::BackgroundRole); else combobox->setItemData(i,QVariant(),Qt::BackgroundRole); } });
EDIT thanks @JonB
-
@VRonin The best way I can explain is through the code. So here is my setup:
QComboBox* m_pComboBox = new QComboBox(this); QSortFilterProxyModel* m_pSortFilterProxyModel = new QSortFilterProxyModel(this); QStringList strList; m_pModel = new QStringListModel(strList, this); m_pSortFilterProxyModel->setSourceModel(m_pModel); m_pComboBox->setModel(m_pSortFilterProxyModel);