When constructing a QFont
instance f
with the default point size of -1 (doc says the point size of the font is set to a system-dependent default value), QFontMetrics {f}.horizontalAdvance()
returns a value which is greater than what I expect.
Here's a complete demo:
class QTextWidthTest :
public QWidget
{
public:
QTextWidthTest()
{
this->setFixedSize(600, 400);
}
private:
void _paintExample(QPainter& painter, const int pointSize,
const QString& text) const
{
const QFont font {"sans-serif", pointSize};
// draw text
painter.setFont(font);
painter.setPen(QColor {"black"});
painter.drawText(0, 0, text);
// draw horizontal advance
painter.setPen(QPen {QColor {"red"}, 2});
painter.drawLine(0, 3, QFontMetricsF {font}.horizontalAdvance(text), 3);
}
void paintEvent(QPaintEvent *) override
{
QPainter painter {this};
const auto sysPtSize = QApplication::font().pointSize();
painter.translate(50, 50);
this->_paintExample(painter, sysPtSize, "meow");
painter.translate(0, 50);
this->_paintExample(painter, sysPtSize, "hello world!");
painter.translate(0, 50);
this->_paintExample(painter, sysPtSize, "hello how are you today?");
painter.translate(0, 100);
this->_paintExample(painter, -1, "meow");
painter.translate(0, 50);
this->_paintExample(painter, -1, "hello world!");
painter.translate(0, 50);
this->_paintExample(painter, -1, "hello how are you today?");
}
};
Here's the result:
Is this a known behaviour or a known bug?