QPushButton highlight.



  • Hi,
    I got problem on windows with my virtual keyboard. It's highlighting of buttons when mouse is on it.
    Example picture:
    Example

    I already tried:
    1)Override onEnter handler with ignore event.
    2)setAutoDefault(false) and setDefault(false)
    3)QPushButton::setFocusPolicy(Qt::NoFocus )
    4)QPushButton::setCheckable(false)

    Nothing works for me.
    Anybody know what I should do?

    Best regards,
    Tomek



  • Hi! You could create your own button class that inherits QPushButton and override the paintEvent like this:

    void MyButton::paintEvent(QPaintEvent *event)
    {
        const QColor backgroundColor = isDown() ? QColor("white") : QColor("grey");
    
        QPainter painter(this);
        painter.setRenderHint(QPainter::Antialiasing);
    
        QPen pen(QColor("orange"));
        pen.setStyle(Qt::SolidLine);
        painter.setPen(pen);
    
        painter.setFont( QFont("Helvetica", 16) );
    
        const QRect rect( event->rect() );
    
        painter.fillRect( rect, QColor("black"));
        painter.fillRect( QRect(rect.left()+1,rect.top()+1,rect.width()-2, rect.height()-2), backgroundColor);
        painter.drawText( rect, Qt::AlignCenter, this->text() );
    }
    

    The result works just like any QPushButton but does no highlighting:



  • It works for me, but do You know maybe a way to achieve similar effect by setStyleSheet function?
    I ask, because I do not want so radically interfere to paint event.
    For example, In Your solution I have to also add some additional code with QFontMetrics to keep proper size of text on buttons.

    Best regards,
    Tomek


  • Qt Champions 2016

    @Wieland
    Isn't it simpler to actually delegate the painting? I.e:

    void MyButton::paintEvent(QPaintEvent *event)
    {
        if (isDown())
            setDown(false);
    
        QPushButton::paintEvent(event);
    }
    


  • No, because button is not highlighted because is down, but because the mouse pointer is on it.


  • Qt Champions 2016

    @Kaluss
    Hello Tomek,
    Then I don't understand how @Wieland's example will work for you. I have not in fact added anything to his suggestion, except for delegating the paint event to the base class ...

    Kind regards.



  • Because in Your example button will be highlighted anyway. Highlight it tuned on when mouse pointer enters on button area. Your example will work when button is pressed and doesn't change anything when pointer will enter button area.


  • Qt Champions 2016

    @Kaluss
    Ugh, sorry, of course you're right! I guess I'm not in shape today. I think there is a way to set it with the stylesheet (your original idea), but I've never worked with those. Maybe something like this:

    QPushButton#myButtonName:hover  {
        background-color: ...
    }
    

    or:

    QPushButton:hover  {
        background-color: ...
    }
    

    might work?



  • Could it be :) I will check when get back to home :)
    setStyleSheet("QPushButton#myButtonName:hover { background-color: transparent}");
    It will work? I ask because I never used this functionality...


  • Qt Champions 2016

    @Kaluss
    It should, I think. Take a peek at the stylesheet examples, I think it'll be more clear than me trying to explain something I've not used ... :)

    Kind regards.



  • I found solutinion:

    void myButton::enterEvent( QEvent *event )
    {
            this->setAttribute(Qt::WA_UnderMouse, false)
    }
    

Log in to reply
 

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