[Interest] QT QFontMetrics boundingRect failed to textwordwrap, thanks!

Tony Rietwyk tony at rightsoft.com.au
Wed Nov 29 23:44:33 CET 2017


Hi,

In your call metrics.boundingRect, you have specified height=0. Doesn't 
that make the QRect invalid?  The help for this function states "The 
drawing, and hence the bounding rectangle, is constrained to the 
rectangle rect." - so you need to pass a very large value for height, 
and it should then work.

Regards, Tony


On 28/11/2017 12:06 PM, sq wrote:
> Hello, in QT4.8 I have the following code, want to calculate the size 
> of the rectangle with the line break, the result of the rect did not 
> wrap at all.
> How can I get the size of rect that a  long QString needed 
> text-wrapped automatically?Thanks very much!
>
>     QPainter painter(this);
>     QString text("1234567890123456789012345678901234567890"
>                  "1234567890123456789012345678901234567890"
>                  "1234567890123456789012345678901234567890"
>                  "1234567890123456789012345678901234567890");
>     int width = this->width()-40;
>     int flags = Qt::TextWordWrap;
> qDebug()<<"font:"<<painter.font().pointSize()<<",pixelSize="<<painter.font().pixelSize();
>
>     QFontMetrics metrics = painter.fontMetrics();
>     QRect textBoundingRect = 
> metrics.boundingRect(QRect(0,0,width,0),flags,text);
>     painter.translate(20,20);
>     painter.drawRect(textBoundingRect);
>     painter.drawText(textBoundingRect,Qt::TextWordWrap,text);
>
>
>
>
>
> _______________________________________________
> Interest mailing list
> Interest at qt-project.org
> http://lists.qt-project.org/mailman/listinfo/interest

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.qt-project.org/pipermail/interest/attachments/20171130/643614c9/attachment.html>


More information about the Interest mailing list