Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Solved Change background for hover item in QFontComboBox?

    General and Desktop
    hover combobox style stylesheet
    2
    7
    1532
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • L
      legitnameyo last edited by legitnameyo

      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

      1 Reply Last reply Reply Quote 0
      • mrjj
        mrjj Lifetime Qt Champion last edited by

        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.

        1 Reply Last reply Reply Quote 1
        • L
          legitnameyo last edited by

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

          mrjj 1 Reply Last reply Reply Quote 0
          • mrjj
            mrjj Lifetime Qt Champion @legitnameyo last edited by

            @legitnameyo
            Yes the Delegate draws the items.
            https://code.woboq.org/qt5/qtbase/src/widgets/widgets/qfontcombobox.cpp.html#_ZNK19QFontFamilyDelegate5paintEP8QPainterRK20QStyleOptionViewItemRK11QModelIndex

            1 Reply Last reply Reply Quote 1
            • L
              legitnameyo last edited by legitnameyo

              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?

              mrjj 1 Reply Last reply Reply Quote 0
              • mrjj
                mrjj Lifetime Qt Champion @legitnameyo last edited by mrjj

                @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 :)

                1 Reply Last reply Reply Quote 2
                • L
                  legitnameyo last edited by

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

                  1 Reply Last reply Reply Quote 1
                  • First post
                    Last post