QComboBox: Disabling mouse hover highlighting
-
@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);