mirror of
https://github.com/Cockatrice/Cockatrice.git
synced 2026-06-24 23:53:54 -07:00
Don't setText() in paintEvent() which causes infinite recursion on MacOs. Use a styleOption to paint base class directly instead. (#6146)
Took 18 minutes Took 5 seconds Took 19 seconds Co-authored-by: Lukas Brübach <Bruebach.Lukas@bdosecurity.de>
This commit is contained in:
parent
aff775f488
commit
9b3756e591
1 changed files with 10 additions and 8 deletions
|
|
@ -3,6 +3,7 @@
|
||||||
#include <QPainter>
|
#include <QPainter>
|
||||||
#include <QPainterPath>
|
#include <QPainterPath>
|
||||||
#include <qgraphicseffect.h>
|
#include <qgraphicseffect.h>
|
||||||
|
#include <qstyleoption.h>
|
||||||
|
|
||||||
HomeStyledButton::HomeStyledButton(const QString &text, QPair<QColor, QColor> _gradientColors, QWidget *parent)
|
HomeStyledButton::HomeStyledButton(const QString &text, QPair<QColor, QColor> _gradientColors, QWidget *parent)
|
||||||
: QPushButton(text, parent), gradientColors(_gradientColors)
|
: QPushButton(text, parent), gradientColors(_gradientColors)
|
||||||
|
|
@ -75,14 +76,15 @@ QString HomeStyledButton::generateButtonStylesheet(const QPair<QColor, QColor> &
|
||||||
|
|
||||||
void HomeStyledButton::paintEvent(QPaintEvent *event)
|
void HomeStyledButton::paintEvent(QPaintEvent *event)
|
||||||
{
|
{
|
||||||
QString originalText = text();
|
Q_UNUSED(event); // Event is just used for update clipping, we redraw the whole widget.
|
||||||
setText(""); // Prevent QPushButton from drawing the text
|
QStyleOptionButton opt;
|
||||||
|
initStyleOption(&opt);
|
||||||
QPushButton::paintEvent(event); // Draw background, borders, etc.
|
opt.text.clear(); // prevent style from drawing text
|
||||||
|
|
||||||
setText(originalText); // Restore text for internal logic
|
|
||||||
|
|
||||||
QPainter painter(this);
|
QPainter painter(this);
|
||||||
|
style()->drawControl(QStyle::CE_PushButton, &opt, &painter, this);
|
||||||
|
|
||||||
|
// Draw white text with a black outline
|
||||||
painter.setRenderHint(QPainter::Antialiasing);
|
painter.setRenderHint(QPainter::Antialiasing);
|
||||||
painter.setRenderHint(QPainter::TextAntialiasing);
|
painter.setRenderHint(QPainter::TextAntialiasing);
|
||||||
|
|
||||||
|
|
@ -91,11 +93,11 @@ void HomeStyledButton::paintEvent(QPaintEvent *event)
|
||||||
painter.setFont(font);
|
painter.setFont(font);
|
||||||
|
|
||||||
QFontMetrics fm(font);
|
QFontMetrics fm(font);
|
||||||
QSize textSize = fm.size(Qt::TextSingleLine, originalText);
|
QSize textSize = fm.size(Qt::TextSingleLine, this->text());
|
||||||
QPointF center((width() - textSize.width()) / 2.0, (height() + textSize.height() / 2.0) / 2.0);
|
QPointF center((width() - textSize.width()) / 2.0, (height() + textSize.height() / 2.0) / 2.0);
|
||||||
|
|
||||||
QPainterPath path;
|
QPainterPath path;
|
||||||
path.addText(center, font, originalText);
|
path.addText(center, font, this->text());
|
||||||
|
|
||||||
painter.setPen(QPen(Qt::black, 2.0, Qt::SolidLine, Qt::RoundCap, Qt::RoundJoin));
|
painter.setPen(QPen(Qt::black, 2.0, Qt::SolidLine, Qt::RoundCap, Qt::RoundJoin));
|
||||||
painter.setBrush(Qt::white);
|
painter.setBrush(Qt::white);
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue