[Qt] Support setting button icon from font glyph
This commit is contained in:
parent
d0f35d0b21
commit
b76c398abe
|
@ -20,6 +20,31 @@ XPushButton::XPushButton(const QIcon& icon, const QString& text,
|
|||
Build();
|
||||
}
|
||||
|
||||
void XPushButton::SetIconFromGlyph(QChar glyph, QColor color, int size) {
|
||||
auto glyph_font = QFont("Segoe MDL2 Assets", size);
|
||||
// Measure the Glyph
|
||||
QFontMetrics measure(glyph_font);
|
||||
QRect icon_rect = measure.boundingRect(glyph);
|
||||
double max = qMax(icon_rect.width(), icon_rect.height());
|
||||
|
||||
// Create the Pixmap
|
||||
// boundingRect can be inaccurate so add a 4px padding to be safe
|
||||
QPixmap pixmap(max + 4, max + 4);
|
||||
pixmap.fill(Qt::transparent);
|
||||
|
||||
// Paint the Glyph
|
||||
QPainter painter(&pixmap);
|
||||
painter.setFont(glyph_font);
|
||||
painter.setRenderHints(QPainter::Antialiasing | QPainter::TextAntialiasing);
|
||||
painter.setPen(QPen(color));
|
||||
|
||||
painter.drawText(pixmap.rect(), Qt::AlignVCenter, glyph);
|
||||
|
||||
painter.end();
|
||||
|
||||
this->setIcon(pixmap);
|
||||
}
|
||||
|
||||
void XPushButton::Build() {
|
||||
setFlat(true);
|
||||
setCursor(Qt::PointingHandCursor);
|
||||
|
|
|
@ -16,6 +16,8 @@ class XPushButton : public Themeable<QPushButton> {
|
|||
XPushButton(const QIcon& icon, const QString& text,
|
||||
QWidget* parent = nullptr);
|
||||
|
||||
void SetIconFromGlyph(QChar glyph, QColor color = Qt::white, int size = 64);
|
||||
|
||||
private:
|
||||
void Build();
|
||||
};
|
||||
|
|
Loading…
Reference in New Issue