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. [SOLVED] QGraphicsItem rotation transform
Forum Updated to NodeBB v4.3 + New Features

[SOLVED] QGraphicsItem rotation transform

Scheduled Pinned Locked Moved General and Desktop
8 Posts 5 Posters 20.3k 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.
  • I Offline
    I Offline
    issam
    wrote on last edited by
    #1

    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 !

    http://www.iissam.com/

    1 Reply Last reply
    0
    • T Offline
      T Offline
      tonygrim
      wrote on last edited by
      #2

      Pseudo:

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

      T.

      Dona nobis pacem

      1 Reply Last reply
      0
      • Chris KawaC Offline
        Chris KawaC Offline
        Chris Kawa
        Lifetime Qt Champion
        wrote on last edited by Chris Kawa
        #3

        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

        1 Reply Last reply
        1
        • I Offline
          I Offline
          issam
          wrote on last edited by
          #4

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

          Thank you both ! ;)

          http://www.iissam.com/

          1 Reply Last reply
          0
          • H Offline
            H Offline
            heatblazer
            wrote on last edited by
            #5

            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.

            1 Reply Last reply
            0
            • H Offline
              H Offline
              heatblazer
              wrote on last edited by
              #6

              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.

              1 Reply Last reply
              0
              • E Offline
                E Offline
                erbalgfx
                wrote on last edited by
                #7

                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?

                1 Reply Last reply
                0
                • E Offline
                  E Offline
                  erbalgfx
                  wrote on last edited by
                  #8

                  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?

                  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