Important: Please read the Qt Code of Conduct -

How to highlight a QWidget

  • Hello everyone! Here is my question:

    I've derived the QToolButton so that buttons in QToolBar can be accessed by tabbing, but when it gains focus it is not highlighted like when i hover with the cursor over it. I've overriden the focusInEvent function, but when it comes to actually highlighting the button I don't know what property/attribute to set or what method to call.

    Here is what it looks like when I hover with the cursor over the button:
    How can I achieve this by replacing the line marked?

    Here is what is currently happening:

    @ void focusInEvent(QFocusEvent* e){
    if(e->gotFocus() && e->reason() == Qt::TabFocusReason){
    //line to be replaced
    setGraphicsEffect(new QGraphicsColorizeEffect());

    Thank you for your help! :)

  • Hover & Focus aren't same thing. If I'm doing that for my self I'll override paintEvent and do the job there - so then it can have visual look as you want.

  • Hi,

    maybe overriding the paintEvent is not necessary. I can't provide a full solution right now, but I know that QtDesigner provides the possibility to give several images for different states of the QToolButton.

    Having a look at the API it seems this functionality is actually provides by the "QIcon": class. From its API:

    A QIcon can generate smaller, larger, active, and disabled pixmaps from the set of pixmaps it is given. Such pixmaps are used by Qt widgets to show an icon representing a particular action.

    So I guess the change in the Toolbutton while hovering is implemented by these means.

    As I said, it is not a full solution, but maybe the easier way to go.

    Hope it helps


    Edit: Thinking more about it, maybe I'm wrong and this frame occurring around the button isn't actually achieved by that QIcon mechanism. In that case you would have to either really use paintEvent. But it might be difficult to reproduce the exact effect using paint operations. Or you would set a new QIcon on your QToolbutton while it is hovered.

  • Hello!

    Thank you for your help!

    I understand that Hover and Focus are 2 different things, but even if I override the paintEvent I still don't know how to repaint the button highlighted. I think that maybe a simpler solution would be to trigger the same paint event like in the hover case, but I wasn't able to do that either.

    I've looked through the properties and methods of QIcon and I didn't find anything helpfull there. :(

    I was able to find the color used for highlighting
    or palette().color(QPalette::Highlight);
    but I don't know how to paint the actual frame using this color.

  • I don't want to be the pooper of parties but I don't think that this is the colour you are looking for. I thing when you highlight some text there is this dark blue colour. Look at the screenshot "here": . I think that's the one you found. I don't even think that it is a colour at all you are looking for in the end. it is more like a visual effect.

  • I think that this color is used for both highlighting bottons and text. You can see that in this "video": at 2:00.

  • Hmm, yes. You are right. Then a lot of alpha has to be involved, because the colour for highlighting of a button seems so much lighter that that for text.

    Anyway, cool video.

  • @AndreiTuicu in overrided paintEvent() you must use QStyleOption/QStyleOptionButton so then you know each state and you can paint your button according to the provided state.

  • @zeljko
    Thanks for the answer! :) If I will succed, I will post the code here.

Log in to reply