Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Change background for hover item in QFontComboBox?
QtWS25 Last Chance

Change background for hover item in QFontComboBox?

Scheduled Pinned Locked Moved Solved General and Desktop
hovercombobox stylestylesheet
7 Posts 2 Posters 2.5k Views
  • 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 Offline
    L Offline
    legitnameyo
    wrote on 29 Apr 2019, 14:49 last edited by legitnameyo
    #1

    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
    0
    • M Offline
      M Offline
      mrjj
      Lifetime Qt Champion
      wrote on 29 Apr 2019, 17:00 last edited by
      #2

      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
      1
      • L Offline
        L Offline
        legitnameyo
        wrote on 29 Apr 2019, 18:22 last edited by
        #3

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

        M 1 Reply Last reply 29 Apr 2019, 18:30
        0
        • L legitnameyo
          29 Apr 2019, 18:22

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

          M Offline
          M Offline
          mrjj
          Lifetime Qt Champion
          wrote on 29 Apr 2019, 18:30 last edited by
          #4

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

          1 Reply Last reply
          1
          • L Offline
            L Offline
            legitnameyo
            wrote on 30 Apr 2019, 18:51 last edited by legitnameyo
            #5

            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?

            M 1 Reply Last reply 30 Apr 2019, 18:59
            0
            • L legitnameyo
              30 Apr 2019, 18:51

              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?

              M Offline
              M Offline
              mrjj
              Lifetime Qt Champion
              wrote on 30 Apr 2019, 18:59 last edited by mrjj
              #6

              @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
              2
              • L Offline
                L Offline
                legitnameyo
                wrote on 30 Apr 2019, 21:53 last edited by
                #7

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

                1 Reply Last reply
                1

                1/7

                29 Apr 2019, 14:49

                • Login

                • Login or register to search.
                1 out of 7
                • First post
                  1/7
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • Users
                • Groups
                • Search
                • Get Qt Extensions
                • Unsolved