Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct
QStaticText::size() bug when using rich text and word wrap
I currently use QPainter:drawText() to draw text inside QGraphicsItem::paint(). It works fine. But I would like to draw rich text, so I have changed QPainter:drawText() to QPainter:drawStaticText(). But I am now finding it impossible to position the text correctly because QStaticText::size() returns the wrong value when I am using QTextOption::WordWrap and Qt::RichText.
QStaticText st( text ); QTextOption options( align ); options.setWrapMode( QTextOption::WordWrap ); st.setTextOption( options ); st.setTextFormat( Qt::RichText ); st.setTextWidth( textRect.width() ); st.prepare( p->worldTransform(), p->font() ); QSizeF sz = st.size(); QPointF pos( textRect.topLeft() ); qreal hOffset = ( textRect.height() - sz.height() ) / 2.0; pos += QPointF( 0.0, hOffset ); p->setClipRect( textRect ); p->drawStaticText( pos, st );
The height value returned QStaticText::size() doesn't match the height of the text displayed on screen. If I remove the word wrap or change it to plain text it works fine. But I need the word wrap and rich text.
I am using Qt 5.9.4 on Windows 7.
Did you try using QStaticText::setTextFormat ?
Yes, it is in the snippet above:
st.setTextFormat( Qt::RichText );
It works fine with Qt::PlainText. But that isn't useful for me.
My bad, I missed it.
Do you also have that with the latest version of Qt ? It should be 5.11
I haven't tried it with 5.11. I tend to avoid the latest release as I value stability over the latest features. I guess I can report it as a bug and maybe they will tell me if/when it is finished. Where do you report Qt bugs these days?
it's nevertheless a good idea to test with the latest version first, in case that bug is already fixed (sometimes strange things happen ;))
also please prepare a minimal example to reproduce your problem. otherwise it's hard for the devs to help you.
edit: and please provide a link to the report here.
I am happy to report that this bug is fixed in Qt5.11.1 on Windows and Mac. Hooray!