QPushButton highlight.
-
Hi,
I got problem on windows with my virtual keyboard. It's highlighting of buttons when mouse is on it.
Example picture:
ExampleI 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 -
@Wieland
Isn't it simpler to actually delegate the painting? I.e:void MyButton::paintEvent(QPaintEvent *event) { if (isDown()) setDown(false); QPushButton::paintEvent(event); }
-
-
@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?
-
@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.