Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

[SOLVED] QGraphicsItem rotation transform



  • Hi all,
    How to rotate a QGraphicsRectItem arround its center not arround it local coordinate origin ?

    in my case : Rect(40, 8) ==> rotation around the point (20,4)
    I tried by setTransformOrigin() but it doesn't work !



  • Pseudo:

    translate(width/2, height/2);
    rotate(angle);
    translate(-(width/2), -(height/2));

    T.


  • Moderators

    setTransformOriginPoint() works just fine:

    #include <QApplication>
    #include <QGraphicsView>
    #include <QGraphicsScene>
    #include <QGraphicsRectItem>
    #include <QPoint>
    #include <QPen>
    
    int main(int argc, char* argv[])
    {
        QApplication a(argc, argv);
    
        QGraphicsScene scene;
        scene.addLine(-100,0,100,0);
        scene.addLine(0,-100,0,100);
        scene.addRect(0 ,0 ,40 ,8);
    
        QGraphicsRectItem* item = scene.addRect(0 ,0 ,40 ,8, QPen(Qt::red));
        item->setTransformOriginPoint(QPoint(20,4));
        item->setRotation(-45);
    
        QGraphicsView gv;
        gv.setScene(&scene);
        gv.show();
        return a.exec();
    }
    

    Result:
    Graphicsview transformation



  • Hi,
    The setTransformOriginPoint() method still not working for me ! :(
    So I used tonygrim's method and it works correctly ! :)

    Thank you both ! ;)



  • I know it`s late to answer but here is a good example "TRANSFORMATIONS" about QGraphicsItem, and if you should be inheriting from it. I had a similar problem, the topic helped a lot.



  • I know it`s late to answer but here is a good example "TRANSFORMATIONS" about QGraphicsItem, and if you should be inheriting from it. I had a similar problem, the topic helped a lot.



  • Hey Guys!

    This is my first post here. :) So my problem is the following:
    I have a QGraphicsScene, there is a QPixmap on it. I would like to rotate that pixmap around a center point, would work like a clock actually.

    I've tried these:

    @
    QPixmap pointer_pixmap("/home/peter/desktop/myimg2.png");
    QTransform transform;

    QGraphicsPixmapItem *pointer = new QGraphicsPixmapItem(pointer_pixmap);
    pointer->setOffset(174,190);
    pointer->setTransformOriginPoint(QPoint(174-pointer_pixmap.width(), 190-pointer_pixmap.height()));
    
    
    transform.translate((174-pointer_pixmap.width())/2,(190-pointer_pixmap.height())/2);
    transform.rotate(60);
    transform.translate(-((174-pointer_pixmap.width())/2),-((190-pointer_pixmap.height())/2));
    
    pointer_pixmap = pointer_pixmap.transformed(transform);
    
    item->addItem(pointer);
    
    pointer->setPixmap(pointer_pixmap);
    

    @

    It looks like the translation doesn't have any effect on my pixmap. Why's that?



  • Hey Guys!

    This is my first post here. :) So my problem is the following:
    I have a QGraphicsScene, there is a QPixmap on it. I would like to rotate that pixmap around a center point, would work like a clock actually.

    I've tried these:

    @
    QPixmap pointer_pixmap("/home/peter/desktop/myimg2.png");
    QTransform transform;

    QGraphicsPixmapItem *pointer = new QGraphicsPixmapItem(pointer_pixmap);
    pointer->setOffset(174,190);
    pointer->setTransformOriginPoint(QPoint(174-pointer_pixmap.width(), 190-pointer_pixmap.height()));
    
    
    transform.translate((174-pointer_pixmap.width())/2,(190-pointer_pixmap.height())/2);
    transform.rotate(60);
    transform.translate(-((174-pointer_pixmap.width())/2),-((190-pointer_pixmap.height())/2));
    
    pointer_pixmap = pointer_pixmap.transformed(transform);
    
    item->addItem(pointer);
    
    pointer->setPixmap(pointer_pixmap);
    

    @

    It looks like the translation doesn't have any effect on my pixmap. Why's that?


Log in to reply