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:
    !http://i63.photobucket.com/albums/h145/andreituicu/example1_zpsf503c411.png(wanted)!
    How can I achieve this by replacing the line marked?

    Here is what is currently happening:
    !http://i63.photobucket.com/albums/h145/andreituicu/example2_zps863598b1.png(currently)!

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

    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":http://qt-project.org/doc/qt-4.8/qicon.html class. From its API:

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

    Soraltan

    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!

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

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

    @all
    I was able to find the color used for highlighting
    palette().highlight().color();
    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":http://qt-project.org/doc/qt-4.8/qpalette.html#ColorRole-enum . 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":http://youtu.be/Xeig34OfJxw?t=2m 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
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.