mirror of
https://github.com/edisionnano/QDiskInfo.git
synced 2026-04-16 19:31:27 +03:00
Resize the box instead of the font
This commit is contained in:
@@ -4,7 +4,26 @@
|
|||||||
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) {
|
||||||
setMinimumHeight(60);
|
setMinimumHeight(60);
|
||||||
setMinimumWidth(100);
|
adjustWidthToFitText();
|
||||||
|
}
|
||||||
|
|
||||||
|
void CustomButton::adjustWidthToFitText() {
|
||||||
|
QFont font = this->font();
|
||||||
|
QFontMetrics fm(font);
|
||||||
|
|
||||||
|
int maxWidth = 0;
|
||||||
|
QString lines[] = {text1, text2, text3};
|
||||||
|
|
||||||
|
for (const QString &line : lines) {
|
||||||
|
int lineWidth = fm.horizontalAdvance(line);
|
||||||
|
if (lineWidth > maxWidth) {
|
||||||
|
maxWidth = lineWidth;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
int desiredWidth = maxWidth + 30;
|
||||||
|
setMinimumWidth(desiredWidth);
|
||||||
|
setMaximumWidth(desiredWidth);
|
||||||
}
|
}
|
||||||
|
|
||||||
void CustomButton::paintEvent(QPaintEvent *event) {
|
void CustomButton::paintEvent(QPaintEvent *event) {
|
||||||
@@ -19,28 +38,34 @@ 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};
|
void CustomButton::resizeEvent(QResizeEvent *event) {
|
||||||
int lineHeight = height() / 3;
|
QPushButton::resizeEvent(event);
|
||||||
|
adjustWidthToFitText();
|
||||||
|
}
|
||||||
|
|
||||||
for (int i = 0; i < 3; ++i) {
|
void CustomButton::setText1(const QString &newText1) {
|
||||||
QFont font = painter.font();
|
if (text1 != newText1) {
|
||||||
int fontSize = 20;
|
text1 = newText1;
|
||||||
font.setPointSize(fontSize);
|
adjustWidthToFitText();
|
||||||
painter.setFont(font);
|
update();
|
||||||
|
}
|
||||||
QFontMetrics fm(font);
|
}
|
||||||
QRect textRect = fm.boundingRect(rect, Qt::AlignLeft, lines[i]);
|
|
||||||
|
void CustomButton::setText2(const QString &newText2) {
|
||||||
while ((textRect.width() > rect.width() || textRect.height() > lineHeight) && fontSize > 1) {
|
if (text2 != newText2) {
|
||||||
fontSize--;
|
text2 = newText2;
|
||||||
font.setPointSize(fontSize);
|
adjustWidthToFitText();
|
||||||
painter.setFont(font);
|
update(); // Repaint the button
|
||||||
fm = QFontMetrics(font);
|
}
|
||||||
textRect = fm.boundingRect(rect, Qt::AlignLeft, lines[i]);
|
}
|
||||||
}
|
|
||||||
|
void CustomButton::setText3(const QString &newText3) {
|
||||||
int yOffset = lineHeight * i + (lineHeight - textRect.height()) / 2;
|
if (text3 != newText3) {
|
||||||
painter.drawText(QRect(rect.left(), yOffset, rect.width(), textRect.height()), Qt::AlignLeft, lines[i]);
|
text3 = newText3;
|
||||||
|
adjustWidthToFitText();
|
||||||
|
update();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -12,6 +12,12 @@ public:
|
|||||||
|
|
||||||
protected:
|
protected:
|
||||||
void paintEvent(QPaintEvent *event) override;
|
void paintEvent(QPaintEvent *event) override;
|
||||||
|
void resizeEvent(QResizeEvent *event) override;
|
||||||
|
void setText1(const QString &newText1);
|
||||||
|
void setText2(const QString &newText2);
|
||||||
|
void setText3(const QString &newText3);
|
||||||
|
void adjustWidthToFitText();
|
||||||
|
|
||||||
|
|
||||||
private:
|
private:
|
||||||
QString text1;
|
QString text1;
|
||||||
|
|||||||
Reference in New Issue
Block a user