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@qt-project.org
http://lists.qt-project.org/mailman/listinfo/interest

_______________________________________________
Interest mailing list
Interest@qt-project.org
http://lists.qt-project.org/mailman/listinfo/interest

Reply via email to