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. QComboBox retains mouse hover highlight if you close popup
Qt 6.11 is out! See what's new in the release blog

QComboBox retains mouse hover highlight if you close popup

Scheduled Pinned Locked Moved Solved General and Desktop
4 Posts 2 Posters 799 Views 3 Watching
  • 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
    Lucianmusat
    wrote on last edited by
    #1

    Hello,

    I am having some issues styling my QComboBoxes. I want to have them highlight when mouse over, but it seems they retain the highlight if I click on them and then close the pop-up (either by clicking outside or by selecting something, does not matter). I am using Qt 5.13.1 and I see this issue on macOS and Linux. Haven't tested on others but I guess it would be the same.

    My style is this simple, maybe I am missing something:

    QComboBox:hover
    {
      background-color: rgba(0, 0, 0, 0.2);
    }
    
    1 Reply Last reply
    1
    • mrjjM Offline
      mrjjM Offline
      mrjj
      Lifetime Qt Champion
      wrote on last edited by mrjj
      #2

      Hi
      So on mac/linux its not doing like this ? (win 10)
      alt text

      1 Reply Last reply
      0
      • L Offline
        L Offline
        Lucianmusat
        wrote on last edited by
        #3

        My stylesheet change make it a light gray color when the mouse hovers on it. When the mouse moves to the outside of it it reverts back to original color. After you click it and then close the popup (either select something or click outside) the combobox stays light gray instead of reverting back.

        1 Reply Last reply
        0
        • L Offline
          L Offline
          Lucianmusat
          wrote on last edited by
          #4

          Ok, I managed to work around this issue, will write it here in case anybody else has this problem. I quit trying to use qss and did it in the code. Subclassed the QComboBox and added the hover events:

          bool TrackableComboBox::event(QEvent *event)
          {
              switch (event->type())
              {
                  case QEvent::Enter:
                      this->setStyleSheet("background-color: rgba(0, 0, 0, 0.2);");
                      break;
                  case QEvent::Leave:
                  case QEvent::MouseButtonPress:
                      this->setStyleSheet("background-color: -1;");
                      break;
                  default:
                      break;
              }
              return QWidget::event(event);
          }
          

          The secret here is to add Leave and MouseButtonPress reset the color, otherwise you will have the same issue.

          1 Reply Last reply
          0

          • Login

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