Fix issues when widget text is too large

This commit is contained in:
Alex Spataru 2021-09-28 19:05:35 -05:00
parent 012df0d57e
commit b61bc7d441
3 changed files with 19 additions and 6 deletions

View File

@ -40,9 +40,6 @@ BaseWidget::BaseWidget()
setPalette(palette);
// Configure label style
QFont font = UI::Dashboard::getInstance()->monoFont();
font.setPixelSize(24);
m_label.setFont(font);
m_label.setAlignment(Qt::AlignHCenter | Qt::AlignVCenter);
// Set stylesheets
@ -57,8 +54,17 @@ BaseWidget::BaseWidget()
void BaseWidget::setValue(const QString &label)
{
if (m_label.text() != label)
// Change label text
if (m_label.text() != label) {
m_label.setText(label);
// Resize label font (so it fits inside the box)
while (QFontMetrics(m_label.font()).horizontalAdvance(label) + 12 > m_label.width()) {
QFont font = m_label.font();
font.setPixelSize(font.pixelSize() - 1);
m_label.setFont(font);
}
}
}
void BaseWidget::setWidget(QWidget *widget, Qt::Alignment alignment, bool autoresize)
@ -91,8 +97,15 @@ void BaseWidget::resizeEvent(QResizeEvent *event)
labelFont.setPixelSize(width / 18);
gaugeFont.setPixelSize(width / 24);
// Set fonts
// Set label font (so it fits inside the box)
m_label.setFont(labelFont);
while (QFontMetrics(m_label.font()).horizontalAdvance(m_label.text()) + 12 > m_label.width()) {
QFont font = m_label.font();
font.setPixelSize(font.pixelSize() - 1);
m_label.setFont(font);
}
// Set widget font
if (m_widget)
m_widget->setFont(gaugeFont);

View File

@ -52,6 +52,7 @@ private:
QWidget *m_widget;
bool m_resizeWidget;
QHBoxLayout m_layout;
int m_labelTextWidth;
};
}

View File

@ -30,7 +30,6 @@ using namespace Widgets;
ExternalWindow::ExternalWindow()
: m_widget(nullptr)
{
m_layout.setContentsMargins(24, 24, 24, 24);
setLayout(&m_layout);
}