mirror of
https://github.com/edisionnano/QDiskInfo.git
synced 2026-04-22 06:03:20 +03:00
Dynamically resize font
This commit is contained in:
@@ -1,4 +1,5 @@
|
|||||||
#include "custombutton.h"
|
#include "custombutton.h"
|
||||||
|
#include <QFontMetrics>
|
||||||
|
|
||||||
CustomButton::CustomButton(const QString &text1, const QString &text2, const QString &text3, const QColor &lineColor, QWidget *parent)
|
CustomButton::CustomButton(const QString &text1, const QString &text2, const QString &text3, const QColor &lineColor, QWidget *parent)
|
||||||
: QPushButton(parent), text1(text1), text2(text2), text3(text3), lineColor(lineColor) {
|
: QPushButton(parent), text1(text1), text2(text2), text3(text3), lineColor(lineColor) {
|
||||||
@@ -18,5 +19,27 @@ void CustomButton::paintEvent(QPaintEvent *event) {
|
|||||||
|
|
||||||
QRect rect(20, 0, width() - 25, height());
|
QRect rect(20, 0, width() - 25, height());
|
||||||
painter.setPen(textColor);
|
painter.setPen(textColor);
|
||||||
painter.drawText(rect, Qt::AlignVCenter | Qt::AlignLeft, text1 + "\n" + text2 + "\n" + text3);
|
|
||||||
|
QString lines[] = {text1, text2, text3};
|
||||||
|
int lineHeight = height() / 3;
|
||||||
|
|
||||||
|
for (int i = 0; i < 3; ++i) {
|
||||||
|
QFont font = painter.font();
|
||||||
|
int fontSize = 1;
|
||||||
|
painter.setFont(font);
|
||||||
|
|
||||||
|
QFontMetrics fm(font);
|
||||||
|
QRect textRect = fm.boundingRect(rect, Qt::AlignLeft, lines[i]);
|
||||||
|
|
||||||
|
while (textRect.width() > rect.width() || textRect.height() > lineHeight) {
|
||||||
|
fontSize--;
|
||||||
|
font.setPointSize(fontSize);
|
||||||
|
painter.setFont(font);
|
||||||
|
fm = QFontMetrics(font);
|
||||||
|
textRect = fm.boundingRect(rect, Qt::AlignLeft, lines[i]);
|
||||||
|
}
|
||||||
|
|
||||||
|
int yOffset = lineHeight * i + (lineHeight - textRect.height()) / 3;
|
||||||
|
painter.drawText(QRect(rect.left(), yOffset, rect.width(), textRect.height()), Qt::AlignLeft, lines[i]);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user