Gui: add function to determine size that a quantity spin box would need to display a certain text

This commit is contained in:
wmayer
2022-06-12 20:01:41 +02:00
parent c2cfccf268
commit ac3390de12
2 changed files with 19 additions and 0 deletions

View File

@@ -785,6 +785,23 @@ void QuantitySpinBox::stepBy(int steps)
selectNumber();
}
QSize QuantitySpinBox::sizeForText(const QString& txt) const
{
const QFontMetrics fm(fontMetrics());
int h = lineEdit()->sizeHint().height();
int w = QtTools::horizontalAdvance(fm, txt);
w += 2; // cursor blinking space
w += iconHeight;
QStyleOptionSpinBox opt;
initStyleOption(&opt);
QSize hint(w, h);
QSize size = style()->sizeFromContents(QStyle::CT_SpinBox, &opt, hint, this)
.expandedTo(QApplication::globalStrut());
return size;
}
QSize QuantitySpinBox::sizeHint() const
{
Q_D(const QuantitySpinBox);

View File

@@ -121,6 +121,8 @@ public:
virtual QValidator::State validate(QString &input, int &pos) const;
virtual void fixup(QString &str) const;
/// This is a helper function to determine the size this widget requires to fully display the text
QSize sizeForText(const QString&) const;
QSize sizeHint() const;
QSize minimumSizeHint() const;
bool event(QEvent *event);