Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Finding the center of QGraphicsPixmapItem
Forum Updated to NodeBB v4.3 + New Features

Finding the center of QGraphicsPixmapItem

Scheduled Pinned Locked Moved General and Desktop
4 Posts 2 Posters 5.6k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    msx_br
    wrote on last edited by
    #1

    Hello,

    I need to apply transformations in one QGraphicsPixmapItem, but i can't find a way to
    do this correctly.

    I don't know how to determine the center of the QGraphicsPixmapItem to apply setTransformOriginPoint and then setTransform.

    @
    MyClass::MyClass(QWidget *parent) : QGraphicsView(parent)
    {
    scene = new QGraphicsScene( this );
    this->setScene(scene);

    QPixmap px( "001.jpg" );
    pItem =  scene->addPixmap( px );
    pItem->setScale( 0.1 );
    pItem->setTransformationMode( Qt::SmoothTransformation );
    

    }

    void MyClass::Rotate( qreal angle )
    {
    qreal old_angle = m_currentAngle; // m_currentAngle is a member var, with the actual angle
    m_currentAngle = angle;
    transform.rotate( m_currentAngle - old_angle, Qt::XAxis );
    pixmapItem->setTransform( transform );
    }
    @

    The rotation occurs, but we can see the wrong "anchor point"...

    msx_br - Brazil (Netherlands)

    1 Reply Last reply
    0
    • L Offline
      L Offline
      ludde
      wrote on last edited by
      #2

      Have you tried using QGraphicsItem::rotate() and/or QGraphicsItem::setTransformOriginPoint()?
      To rotate around the center of the bounding rect, I would assume you should do something like
      @pixmapItem->setTransformOriginPoint(pixmapItem->boundingRect().center());@

      1 Reply Last reply
      0
      • M Offline
        M Offline
        msx_br
        wrote on last edited by
        #3

        Thank you ludde,

        I tried you suggestion, but the rotation has the originPoint centered at the topLeft corner.
        ( boundingRect().center() have no effect ) maybe we found a bug?

        What resolved the case:

        @
        transform.translate( x, y );
        transform.rotate( angle - old_angle );
        transform.translate( -x, -y );
        scene->items().at(0)->setTransform( transform );
        @

        Thank you again.

        msx_br - Brazil (Netherlands)

        1 Reply Last reply
        0
        • L Offline
          L Offline
          ludde
          wrote on last edited by
          #4

          OK, I think this is because you are creating a transformation instead of using the QGraphicsItem::rotate() function. If you create and set a transformation you probably have to do the translation (moving to the center and back) yourself. So probably not a bug, but could be better documented.
          Anyway, good that you found a way that works, even if it was not exactly what I suggested. :)

          1 Reply Last reply
          0

          • Login

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • Users
          • Groups
          • Search
          • Get Qt Extensions
          • Unsolved