Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Disable highlighting when hovering on QListWidget item?



  • I'm trying to disable the highlighting of a QListWidget item when hovering over it. I came across two solutions online, as listed here.

    Here's my code to apply the style sheet:

    main.cpp

        app.setStyleSheet("QListWidget::item:hover {background: transparent;}");
        app.setStyleSheet("QListWidget::item:disabled:hover {background: transparent;}");
        app.setStyleSheet("QListWidget::item:hover:!active {background: transparent;}");
    

    I tried the first one, and it works partially but is buggy. In the following screenshots, the right-most of four list widgets has an item in it selected. When I hover over an item in the fourth list widget, the highlighting is still there (as shown in the first screenshot), however, when I hover over one of the other three list widgets, the highlighting is disabled as expected. Additionally, when hovering over another item that is selected (as shown in the second screenshot), the background turns white, instead of staying gray like I want it to (I have all four list widgets set the row to the current row of the item that is clicked in any of the 4 list widgets).


    As for the other method that SGaist suggested, when I try overriding the paint function, I get the error only virtual member functions can be marked 'override'

    debugger.h (click to view full source. Ignore the line being commented)

    #include <QStyledItemDelegate>
    
    protected:
        void paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const override;
    

    So I couldn't figure out how to override the function as done here. (I did realize that the function in QStyledItemDelegate isn't protected but public, but putting it under public: didn't make a difference.)

    What I want exactly is for no item in the list widgets to have highlighting when hovered over, and the selected row/item to remain gray when both selected and hovered over; I don't what anything to be highlighted blue when selected or hovered over.


  • Lifetime Qt Champion

    @adalovegirls said in Disable highlighting when hovering on QListWidget item?:

    when I try overriding the paint function,

    You have to derive from QStyledItemDelegate, unset State_MouseOver in the paint() function and call the base class impl.



  • Thanks. I'll see if I can figure out how to do any of that.


Log in to reply