Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

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 Still stuck any ideas?



  • @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.



  • 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?



  • @anshah

    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 can

    QObject::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

            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?



  • @VRonin

    In addition to that, you can emit comboBox->currentIndexChanged(0); to paint the default item's background green.
    Otherwise it only works after changing the currentItem.



  • @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);
    

Log in to reply