Important: Please read the Qt Code of Conduct -

QGraphicsTextItem bounding rect is incorrect

  • Hi there,

    I'm having some trouble rotating a QGraphicsTextItem around its center. The software I am developing allows the user to create labels in a scene by clicking the position they want the label and an editable QGraphicsTextItem appears. This enables them to edit the label as it would look when they have finished editing.

    I create the label and set its location and rotation as follows, before adding it to the scene:

    @ // m_labelTextItem is a QGraphicsTextItem
    m_labelTextItem->setPos(m_pickpoint.x() + offset.x(),m_pickpoint.y() + offset.y());
    m_labelTextItem->setRotation( get2DTextItemRotation() );@

    This code positions the QGraphicsTextItem where I want it correctly, and allows it to be rotated, however the rotation is not around the center of the QGraphicsTextItem as it is rendered:

    ! rotation)!

    This image shows the same QGraphicsTextItem centered on the red X then rotated at 0, 45 and 90 degrees. Each time I took a screen shot and overlaid the images over each other. Clearly, the transform origin point has not been set to the center of the rendered object but is somewhere in the base of the 'L'.

    I have not provided a new boundingRect function, the QGraphicsTextItem uses the one reimplemented from QGraphicsItem, as described in the documentation.

    If anyone could advise me whether I'm am doing something wrong here, misunderstanding the documentation or let me know if this is a bug I would be really grateful!


  • I actually have another example of the QGraphicsTextItem (QGTI) boudningRect being incorrectly calculated.

    In this case I am concerned with the alignment of the QGTI relative to the position it should be drawn at.

    ! and correct alignment of the QGraphicsTextItem)!

    These QGTIs have all been positioned at the red X and then offset by the width as follows:

    @ double width = m_labelTextItem->boundingRect().width();
    double height = m_labelTextItem->boundingRect().height();

    QPoint offset;

    switch (m_alignment)
    case Label::TOP_LEFT:
    offset.setY( 0 );
    offset.setX( 0 );
    case Label::BOTTOM_LEFT:
    offset.setY( (int) -height );
    offset.setX( 0 );
    case Label::TOP_RIGHT:
    offset.setY( 0 );
    offset.setX( (int) ( -width) );
    case Label::BOTTOM_RIGHT:
    offset.setY( (int) ( -height) );
    offset.setX( (int) ( -width) );

    QTransform offsetTransform;
    offsetTransform.translate(offset.x(), offset.y());

    m_labelTextItem->setPos(m_pickpoint.x(), m_pickpoint.y());
    m_labelTextItem->setTransform(offsetTransform, false);@

    Using the boundingRect width and height is what causes the incorrect result in the left, with the additional space below and to the right of QGTIs.

    If I map the top left and bottom right corners to the scene, then calculate the width and heights manually, I can get the desired result be subtracting these values from the position as required:

    @ QPointF topLeft = graphicsView->mapToScene(
    (int) m_labelTextItem->boundingRect().topLeft().x(),
    (int) m_labelTextItem->boundingRect().topLeft().y() );
    QPointF bottomRight = graphicsView->mapToScene(
    (int) m_labelTextItem->boundingRect().bottomRight().x(),
    (int) m_labelTextItem->boundingRect().bottomRight().y() );
    double width = bottomRight.x() - topLeft.x();
    double height = bottomRight.y() - topLeft.y();

    switch (m_alignment) { < snip > }

    m_labelTextItem->setPos(m_pickpoint.x() + offset.x(),m_pickpoint.y() + offset.y());@

    It is this code that produces the correct result on the right.

    I should add that the amount of extra space added below and to the right is not fixed, it increases as you add more text and vice versa.

  • Moderators

    If you think this is a bug, then please "file a bug report": .

    It must have taken a lot of time to get all this researched and written down, it would be a waste for this to not get the attention of the responsible developer. In the bugtracker such attention is guaranteed, here it is unfortunately not.

Log in to reply