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

Change background for hover item in QFontComboBox?



  • I want to change the background to a grey color of the item that the mouse hovers over in a QFontComboBox. I've tried with all of these styles but none works

        combo_box_fonts->setStyleSheet(QString("QFontComboBox {background-color: white; color: black; border: 0px solid white; border-radius: 3px;}"
    
    "QFontComboBox QAbstractItemView {background-color: white; color: black;}"
    
    "QFontComboBox QAbstractItemView::item:hover {background: #d9d9d9; color: black;}"
    
    "QFontComboBox QListView::item:hover {background: #d9d9d9; color: black;}"
    
    "QListView::item:hover {background: #d9d9d9; color: black;}"
    
    "QFontComboBox QListView:hover {background: #d9d9d9; color: black;}
    
     "QFontComboBox::item:hover {background: #d9d9d9; color: black;}"
    
    "QFontComboBox:on:hover {background: #d9d9d9; color: black;}"));
    

    I've tried all of these styles by themselves and all of these styles together, but the background color of the hovered item is always red.

    Edit: There is no error and every style I've applied have worked, besides :hover


  • Lifetime Qt Champion

    Hi
    Using
    ui->fontComboBox->dumpObjectTree();
    we can see the object tree.

    QFontComboBox::fontComboBox 
        QLineEdit:: 
            QWidgetLineControl:: 
            QCompleter:: 
                QCompletionModel:: 
        QComboBoxPrivateContainer:: 
            QBoxLayout:: 
            QComboBoxListView:: 
                QWidget::qt_scrollarea_viewport 
                QWidget::qt_scrollarea_hcontainer 
                    QScrollBar:: 
                    QBoxLayout:: 
                QWidget::qt_scrollarea_vcontainer 
                    QScrollBar:: 
                    QBoxLayout:: 
                QItemSelectionModel:: 
                QItemSelectionModel:: 
        QStringListModel:: 
        QFontFamilyDelegate:: 
    

    We can see its a QComboBoxListView
    and doing
    QComboBoxListView {background: red}
    does change its background
    alt text

    However, the items are drawn using a delegate (QFontFamilyDelegate) and it seems it does not care for ::hover setting.



  • I tried styling it with

    *:hover {color: red;}
    

    No text turned red when I hovered. I guess that means that the background color of the item hovered is controlled by something else...


  • Lifetime Qt Champion



  • So my theory is, @mrjj, that I have to change the source code of where the item is changed and then build Qt from source, is that right? I've got no other alternative?


  • Lifetime Qt Champion

    @legitnameyo
    well you also take the delegate code and reuse
    that to make a version where it draws as you want.
    (using option.state & QStyle::State_MouseOver ) to react to hover.
    and set you delegate with ui->fontComboBox->setItemDelegate.

    Update:
    yep very possible.
    alt text

    .h file with the code
    https://www.dropbox.com/s/9g85hpxyle0zq52/qfontfamilydelegate.h?dl=0
    include that in project and do
    ui->fontComboBox->setItemDelegate(new QFontFamilyDelegate(this));

    Disclaimer, i used 5 mins on it. so the actual hover drawing needs more love.
    It was just a check to verify it was indeed possible :)



  • @mrjj your file works like a charm! Thanks for the help!