int SubWindow::minimum_width() const
{
QFontMetrics m(info_label_body_->font());
- const int label_width = m.width(QString(tr(initial_notice)));
+ const int label_width = util::text_width(m, tr(initial_notice));
return label_width + min_width_margin;
}
QString s;
QTextStream ts(&s);
if (sign && !v.is_zero())
- ts << forcesign;
+ ts.setNumberFlags(ts.numberFlags() | QTextStream::ForceSign);
ts << qSetRealNumberPrecision(precision) << (v * multiplier);
ts << ' ' << prefix << unit;
QString s;
QTextStream ts(&s);
if (sign && (v != 0))
- ts << forcesign;
+ ts.setNumberFlags(ts.numberFlags() | QTextStream::ForceSign);
ts.setRealNumberNotation(QTextStream::FixedNotation);
ts.setRealNumberPrecision(precision);
ts << (v * multiplier) << ' ' << prefix << unit;
return result;
}
+/**
+ * Return the width of a string in a given font.
+ *
+ * @param[in] metric metrics of the font
+ * @param[in] string the string whose width should be determined
+ *
+ * @return width of the string in pixels
+ */
+std::streamsize text_width(const QFontMetrics &metric, const QString &string)
+{
+#if QT_VERSION >= QT_VERSION_CHECK(5, 11, 0)
+ return metric.horizontalAdvance(string);
+#else
+ return metric.width(string);
+#endif
+}
+
} // namespace util
} // namespace pv
#include <QMetaType>
#include <QString>
+#include <QFontMetrics>
using std::string;
using std::vector;
vector<string> split_string(string text, string separator);
+/**
+ * Return the width of a string in a given font.
+ * @param[in] metric metrics of the font
+ * @param[in] string the string whose width should be determined
+ *
+ * @return width of the string in pixels
+ */
+std::streamsize text_width(const QFontMetrics &metric, const QString &string);
+
} // namespace util
} // namespace pv
// Determine shortest string we want to see displayed in full
QFontMetrics m(QApplication::font());
- min_useful_label_width_ = m.width("XX"); // e.g. two hex characters
+ // e.g. two hex characters
+ min_useful_label_width_ = util::text_width(m, "XX");
default_row_height_ = (ViewItemPaintParams::text_height() * 6) / 4;
annotation_height_ = (ViewItemPaintParams::text_height() * 5) / 4;
const int rightedge = width();
const int x_tick = tick.first;
if ((x_tick > leftedge) && (x_tick < rightedge)) {
- const int x_left_bound = QFontMetrics(font()).width(tick.second) / 2;
+ const int x_left_bound = util::text_width(QFontMetrics(font()), tick.second) / 2;
const int x_right_bound = rightedge - x_left_bound;
const int x_legend = min(max(x_tick, x_left_bound), x_right_bound);
p.drawText(x_legend, ValueMargin, 0, text_height,
{
const QFontMetrics fm(fontMetrics());
const int l = round(value_).str().size() + precision_ + 10;
- const int w = fm.width(QString(l, '0'));
+ const int w = util::text_width(fm, QString(l, '0'));
const int h = lineEdit()->minimumSizeHint().height();
return QSize(w, h);
}