Overlay in QPainter, text needs to be resized as window resizes
Unsolved
General and Desktop
-
Hi,
I have overlay text in QPainter ,i need to resize the overlay text when the window resizes, right now the text is of same size even if i resize the window.
QPainter painter(this); QRect rect(this->m_pQWidget->x(), this->m_pQWidget->y(), this->m_pQWidget->width(), this->m_pQWidget->height()); painter.fillRect(rect,Qt::black); QDateTime dateTimeObject; QPen oPen1; oPen1.setWidth(1); oPen1.setColor("green"); painter.setPen(oPen1); painter.drawText(rect,Qt::AlignBottom | Qt::AlignCenter, dateTimeObject.currentDateTime().toString());
How can i achieve this?.
Thanks,
-
@Pradeep-Kumar
Hi
Use http://doc.qt.io/qt-5/qfontmetrics.html
to know size of text, then use
the font to size it by using a larger point size. -
QFontMetrics fm = painter.fontMetrics(); int width = fm.width(dateTimeObject.currentDateTime().toString());
Is this the way to use QFontMetrics?.
And needs to be updated in paintevent()?.
Thanks,
-
@Pradeep-Kumar
well yes, you can use that information to know
if you should increase or decrease font size. -
Then after i got width from QFontMetrics, we need to add to Qpen setwidth?.
-
@Pradeep-Kumar
No QFont :)
Draw text smaller or bigger
http://doc.qt.io/qt-5/qfont.html#setPointSize