How can I wrap text in QGraphicsItem?

5.9k views Asked by At

1) How can I wrap text in a QGraphicsTextItem to fit a fixed rectangle, with width and height ?

Right now I am experimenting with creating a text, getting its bounding rectangle, and resizing it to fit the box - but I can't get wrapping.

class TTT: public QGraphicsTextItem {
    TTT() {
    {
        setPlainText("abcd");
        qreal x = m_itemSize.width()/boundingRect().width();
        qreal y = m_itemSize.height()/boundingRect().height();
        scale(x, y);
    }
    void paint(QPainter* painter, const QStyleOptionGraphicsItem* option, QWidget* widget) {
        // experiment with clip regions
        // text gets covered by hole in clip
        QRegion r0(boundingRect().toRect());
        QRegion r1(QRect(5, 5, 10, 10), QRegion::Ellipse);
        QRegion r2 = r0.subtracted(r1);
        painter->setClipRegion(r2);
        painter->setBrush(Qt::yellow);
        painter->drawRect(boundingRect());
        QGraphicsTextItem::paint(painter, option, widget);
    }
}

What makes wrapping happen, how can I trigger it ?

Right now, as I keep typing, the box is automatically expanding.

2) Is it possible to wrap the text in a QGraphicsItem / QGraphicTextItem subclass in a shape that is not a rectangle ? enter image description here

(Something like in the image above)
I tried to use clipRegion, see code above, but I guess it is not the right way to go, clipping cuts the text but did not wrap.

Maybe it would... If I could figure out how to wrap text in the first place ?

Qt 4.8

2

There are 2 answers

0
Adam Finley On BEST ANSWER

You did not specify Qt version but try:

void QGraphicsTextItem::setTextWidth(qreal width)

Sets the preferred width for the item's text. If the actual text is wider than >the specified width then it will be broken into multiple lines.

If width is set to -1 then the text will not be broken into multiple lines >unless it is enforced through an explicit line break or a new paragraph.

The default value is -1.

6
TheDarkKnight On

In answer to 1) I'd opt not to use the QGraphicsTextItem, but draw the text directly in your QGraphicsItem's paint function using the drawText overloaded function, which takes a QTextOption parameter.

Using this, you can set the WrapMode, for example, with a call to

QTextOption::setWrapMode(QTextOption:: WordWrap)

As for 2) with a non-rectangular shape, I don't think Qt will do this for you.

Doing it yourself you can use QFontMetrics, to work out just how much text would fit in each line, depending upon where it lies within its bounding item.

Alternatively, you could adapt the concept of a text-to-path method.