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