Trouble Aligning QGraphicsTextItem when drawing it to a Qimage



  • I'm taking a QGraphicsTextItem and drawing it to a QImage. I want to draw it directly underneath the QGraphicsTextItem, but am running into problems with the right align flag or combination of flags. Any Suggestions?

    @
    void drawMyText(const QGraphicsTextItem &txtItem)
    {
    QPainter painter(&myQImage);
    painter.setPen(myPen);
    painter.setBrush(myBrush);
    painter.setFont(txtItem.font());
    painter.setTransform(txtItem.sceneTransform());

    // the below is really close but is not directly underneath the text item
    painter.drawText(txtItem.boundingRect(), Qt::AlignCenter, txtItem.toPlainText());

    }

    @



  • I found the right combination for my situation was combining AlignCenter with AlignJustify as shown below.

    @ void drawMyText(const QGraphicsTextItem &txtItem)
    {
    QPainter painter(&myQImage);
    painter.setPen(myPen);
    painter.setBrush(myBrush);
    painter.setFont(txtItem.font());
    painter.setTransform(txtItem.sceneTransform());

    // AlignCenter and AlignJustify works for me.
    painter.drawText(txtItem.boundingRect(), Qt::AlignCenter | Qt::AlignJustify , txtItem.toPlainText());
     
    }
    

    @


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.