Unsolved QComboBox: Disabling mouse hover highlighting
-
@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.
-
This post is deleted! -
This post is deleted! -
@Pl45m4 I like your idea about the check icon for the selected item. How can I go about doing this?
-
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
-
if(i==idx) combobox->setItemData(i,QBrush(Qt::green),Qt::BackgroundRole); combobox->setItemData(i,QVariant(),Qt::BackgroundRole);
Do you mean either an
else
in your code or swap the order of the two lines? -
In addition to that, you can
emit comboBox->currentIndexChanged(0);
to paint the default item's background green.
Otherwise it only works after changing thecurrentItem
. -
@Pl45m4 Much appreciated. I tried using a check mark PNG image and calling setItemIcon(index, icon) on the QComboBox but I still don't see the icon.
-
@VRonin Unfortunately we do call setModel. We set QSortFilterProxyModel to the ComboBox. How does the above connect change with that?
-
@Pl45m4 Where do I put the emit? After the if-else?
emit comboBox->currentIndexChanged(0);
-
@anshah said in QComboBox: Disabling mouse hover highlighting:
Where do I put the emit?
After initialization. Or you set your first item's background to "green" manually.
-
@anshah said in QComboBox: Disabling mouse hover highlighting:
How does the above connect change with that?
What's the underlying model you are using (I mean after all the proxies)?
-
@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);