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

sq vitasr at 163.com
Tue Nov 28 02:06:18 CET 2017


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);
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.qt-project.org/pipermail/interest/attachments/20171128/5f216177/attachment.html>


More information about the Interest mailing list