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: Disabling mouse hover highlighting
Forum Updated to NodeBB v4.3 + New Features

QComboBox: Disabling mouse hover highlighting

Scheduled Pinned Locked Moved Unsolved General and Desktop
18 Posts 4 Posters 3.2k Views 1 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.
  • A Offline
    A Offline
    anshah
    wrote on last edited by anshah
    #1

    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?

    A Pl45m4P 2 Replies Last reply
    0
    • A anshah

      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?

      A Offline
      A Offline
      anshah
      wrote on last edited by
      #2

      @anshah Still stuck any ideas?

      1 Reply Last reply
      0
      • A anshah

        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?

        Pl45m4P Offline
        Pl45m4P Offline
        Pl45m4
        wrote on last edited by Pl45m4
        #3

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


        If debugging is the process of removing software bugs, then programming must be the process of putting them in.

        ~E. W. Dijkstra

        1 Reply Last reply
        0
        • A Offline
          A Offline
          anshah
          wrote on last edited by
          #4

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

          Pl45m4P VRoninV 2 Replies Last reply
          0
          • A anshah

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

            Pl45m4P Offline
            Pl45m4P Offline
            Pl45m4
            wrote on last edited by Pl45m4
            #5

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


            If debugging is the process of removing software bugs, then programming must be the process of putting them in.

            ~E. W. Dijkstra

            A 2 Replies Last reply
            0
            • Pl45m4P Pl45m4

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

              A Offline
              A Offline
              anshah
              wrote on last edited by
              #6
              This post is deleted!
              A 1 Reply Last reply
              0
              • A anshah

                This post is deleted!

                A Offline
                A Offline
                anshah
                wrote on last edited by
                #7
                This post is deleted!
                1 Reply Last reply
                0
                • Pl45m4P Pl45m4

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

                  A Offline
                  A Offline
                  anshah
                  wrote on last edited by
                  #8

                  @Pl45m4 I like your idea about the check icon for the selected item. How can I go about doing this?

                  Pl45m4P 1 Reply Last reply
                  0
                  • A anshah

                    @Pl45m4 I like your idea about the check icon for the selected item. How can I go about doing this?

                    Pl45m4P Offline
                    Pl45m4P Offline
                    Pl45m4
                    wrote on last edited by Pl45m4
                    #9

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


                    If debugging is the process of removing software bugs, then programming must be the process of putting them in.

                    ~E. W. Dijkstra

                    A 1 Reply Last reply
                    1
                    • A anshah

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

                      VRoninV Offline
                      VRoninV Offline
                      VRonin
                      wrote on last edited by VRonin
                      #10

                      @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

                      "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
                      ~Napoleon Bonaparte

                      On a crusade to banish setIndexWidget() from the holy land of Qt

                      JonBJ Pl45m4P A 3 Replies Last reply
                      3
                      • VRoninV VRonin

                        @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

                        JonBJ Offline
                        JonBJ Offline
                        JonB
                        wrote on last edited by JonB
                        #11

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

                        1 Reply Last reply
                        3
                        • VRoninV VRonin

                          @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

                          Pl45m4P Offline
                          Pl45m4P Offline
                          Pl45m4
                          wrote on last edited by Pl45m4
                          #12

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


                          If debugging is the process of removing software bugs, then programming must be the process of putting them in.

                          ~E. W. Dijkstra

                          A 1 Reply Last reply
                          0
                          • Pl45m4P Pl45m4

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

                            A Offline
                            A Offline
                            anshah
                            wrote on last edited by
                            #13

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

                            1 Reply Last reply
                            0
                            • VRoninV VRonin

                              @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

                              A Offline
                              A Offline
                              anshah
                              wrote on last edited by
                              #14

                              @VRonin Unfortunately we do call setModel. We set QSortFilterProxyModel to the ComboBox. How does the above connect change with that?

                              VRoninV 1 Reply Last reply
                              0
                              • Pl45m4P Pl45m4

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

                                A Offline
                                A Offline
                                anshah
                                wrote on last edited by
                                #15

                                @Pl45m4 Where do I put the emit? After the if-else?

                                emit comboBox->currentIndexChanged(0);
                                
                                Pl45m4P 1 Reply Last reply
                                0
                                • A anshah

                                  @Pl45m4 Where do I put the emit? After the if-else?

                                  emit comboBox->currentIndexChanged(0);
                                  
                                  Pl45m4P Offline
                                  Pl45m4P Offline
                                  Pl45m4
                                  wrote on last edited by
                                  #16

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


                                  If debugging is the process of removing software bugs, then programming must be the process of putting them in.

                                  ~E. W. Dijkstra

                                  1 Reply Last reply
                                  0
                                  • A anshah

                                    @VRonin Unfortunately we do call setModel. We set QSortFilterProxyModel to the ComboBox. How does the above connect change with that?

                                    VRoninV Offline
                                    VRoninV Offline
                                    VRonin
                                    wrote on last edited by
                                    #17

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

                                    "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
                                    ~Napoleon Bonaparte

                                    On a crusade to banish setIndexWidget() from the holy land of Qt

                                    A 1 Reply Last reply
                                    0
                                    • VRoninV VRonin

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

                                      A Offline
                                      A Offline
                                      anshah
                                      wrote on last edited by
                                      #18

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