Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. How to prevent a QIcon from getting highlighted?
Qt 6.12 LTS is out! See what's new in the release blog

How to prevent a QIcon from getting highlighted?

Scheduled Pinned Locked Moved Unsolved General and Desktop
10 Posts 5 Posters 1.7k Views 2 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.
  • D Offline
    D Offline
    Daniella
    wrote on last edited by Daniella
    #1

    I'm trying to prevent the icons set on a QComboBox to get highlighted when the mouse is currently over the item.

    I tried to use QStyleItemDelegate and ignore the flag MouseOver but this approach stops the painter from painting the 'hovered' background of the item and it still draw the icon highlight.

    What else i could try?

    class ComboBoxDelegate : public QStyledItemDelegate
    {
        Q_OBJECT
    public:
        QComboBox* comboBox = nullptr;
        ComboBoxDelegate(QComboBox* comboBox) : QStyledItemDelegate(comboBox), comboBox(comboBox) {};
    
        void paint(QPainter* painter, const QStyleOptionViewItem& option,
            const QModelIndex& index) const override
        {
            QStyleOptionViewItem opt = option;
            // Check if the current item is hovered
            if (opt.state & QStyle::State_MouseOver)
                opt.state = QStyle::State_None;
    
            return QStyledItemDelegate::paint(painter, opt, index);
        }
    };
    
    
    
    class ComboBox : public QComboBox
    {
    public:
        ComboBoxDelegate* comboBoxDelegate = nullptr;
        ComboBox(QWidget *parent = nullptr) : QComboBox(parent)
        {
            comboBoxDelegate = new ComboBoxDelegate(this);
            setItemDelegate(comboBoxDelegate);
        }
    }
    
    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi,

      The next step is to do the painting yourself.

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      D 1 Reply Last reply
      0
      • SGaistS SGaist

        Hi,

        The next step is to do the painting yourself.

        D Offline
        D Offline
        Daniella
        wrote on last edited by
        #3

        @SGaist isnt possible to disable the highlighting editing any option?

        B 1 Reply Last reply
        0
        • D Daniella

          @SGaist isnt possible to disable the highlighting editing any option?

          B Offline
          B Offline
          Bonnie
          wrote on last edited by
          #4

          @Daniella Can't quite understand what does "highlighted" QIcon mean.
          Could you explain better?

          D 1 Reply Last reply
          0
          • B Bonnie

            @Daniella Can't quite understand what does "highlighted" QIcon mean.
            Could you explain better?

            D Offline
            D Offline
            Daniella
            wrote on last edited by
            #5

            @Bonnie add an icon to a QComboBox and move the mouse over it, the icon is highlighted/colored blue.

            https://doc.qt.io/qt-6/qstyleoptionviewitem.html#showDecorationSelected-var

            "This variable holds whether the decoration should be highlighted on selected items"

            Even setting it false, it still highlights the icon, a bug?

            class ComboBoxDelegate : public QStyledItemDelegate
            {
                Q_OBJECT
            public:
                QComboBox* comboBox = nullptr;
                ComboBoxDelegate(QComboBox* comboBox) : QStyledItemDelegate(comboBox), comboBox(comboBox) {};
            
                void paint(QPainter* painter, const QStyleOptionViewItem& option,
                    const QModelIndex& index) const override
                {
                    QStyleOptionViewItem opt = option;
                    if (opt.showDecorationSelected)
                        opt.showDecorationSelected = false;
            
                    return QStyledItemDelegate::paint(painter, opt, index);
                }
            };
            
            SGaistS JoeCFDJ 2 Replies Last reply
            0
            • D Daniella

              @Bonnie add an icon to a QComboBox and move the mouse over it, the icon is highlighted/colored blue.

              https://doc.qt.io/qt-6/qstyleoptionviewitem.html#showDecorationSelected-var

              "This variable holds whether the decoration should be highlighted on selected items"

              Even setting it false, it still highlights the icon, a bug?

              class ComboBoxDelegate : public QStyledItemDelegate
              {
                  Q_OBJECT
              public:
                  QComboBox* comboBox = nullptr;
                  ComboBoxDelegate(QComboBox* comboBox) : QStyledItemDelegate(comboBox), comboBox(comboBox) {};
              
                  void paint(QPainter* painter, const QStyleOptionViewItem& option,
                      const QModelIndex& index) const override
                  {
                      QStyleOptionViewItem opt = option;
                      if (opt.showDecorationSelected)
                          opt.showDecorationSelected = false;
              
                      return QStyledItemDelegate::paint(painter, opt, index);
                  }
              };
              
              SGaistS Offline
              SGaistS Offline
              SGaist
              Lifetime Qt Champion
              wrote on last edited by
              #6

              I might be wrong but if I understand the documentation correctly, it seems to talk about the drawing of things like the tree view branches rather than what is returned by the decoration role.

              Interested in AI ? www.idiap.ch
              Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

              1 Reply Last reply
              0
              • D Daniella

                @Bonnie add an icon to a QComboBox and move the mouse over it, the icon is highlighted/colored blue.

                https://doc.qt.io/qt-6/qstyleoptionviewitem.html#showDecorationSelected-var

                "This variable holds whether the decoration should be highlighted on selected items"

                Even setting it false, it still highlights the icon, a bug?

                class ComboBoxDelegate : public QStyledItemDelegate
                {
                    Q_OBJECT
                public:
                    QComboBox* comboBox = nullptr;
                    ComboBoxDelegate(QComboBox* comboBox) : QStyledItemDelegate(comboBox), comboBox(comboBox) {};
                
                    void paint(QPainter* painter, const QStyleOptionViewItem& option,
                        const QModelIndex& index) const override
                    {
                        QStyleOptionViewItem opt = option;
                        if (opt.showDecorationSelected)
                            opt.showDecorationSelected = false;
                
                        return QStyledItemDelegate::paint(painter, opt, index);
                    }
                };
                
                JoeCFDJ Offline
                JoeCFDJ Offline
                JoeCFD
                wrote on last edited by
                #7

                @Daniella did you try stylesheet?
                https://stackoverflow.com/questions/37765673/changing-hover-style-for-qcombobox

                D 1 Reply Last reply
                0
                • JoeCFDJ JoeCFD

                  @Daniella did you try stylesheet?
                  https://stackoverflow.com/questions/37765673/changing-hover-style-for-qcombobox

                  D Offline
                  D Offline
                  Daniella
                  wrote on last edited by Daniella
                  #8

                  @JoeCFD i have tried it now, it still highlight the icon.

                  The problem is not the text/item background, is the icon, it create a blue overlay (highlight) over the icons, and make difficult to understand/see them.

                  @SGaist see the comment of this question

                  JoeCFDJ 1 Reply Last reply
                  0
                  • D Daniella

                    @JoeCFD i have tried it now, it still highlight the icon.

                    The problem is not the text/item background, is the icon, it create a blue overlay (highlight) over the icons, and make difficult to understand/see them.

                    @SGaist see the comment of this question

                    JoeCFDJ Offline
                    JoeCFDJ Offline
                    JoeCFD
                    wrote on last edited by
                    #9

                    @Daniella how did you do it? set background color to green to see if you can change the color.

                    1 Reply Last reply
                    0
                    • V Offline
                      V Offline
                      Verz Arius
                      wrote on last edited by Verz Arius
                      #10
                      QIcon icon(":/path_to_icon");
                      icon.addFile(":/path_to_icon", {}, QIcon::Selected);
                      listItem->setIcon(icon);
                      

                      Tested on qt 5.15 in QListWidget.

                      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
                      • Unsolved