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. Rotate QRectF
Qt 6.11 is out! See what's new in the release blog

Rotate QRectF

Scheduled Pinned Locked Moved Solved General and Desktop
10 Posts 4 Posters 5.9k 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.
  • Gianluca86G Offline
    Gianluca86G Offline
    Gianluca86
    wrote on last edited by
    #1

    Hi,
    I have a QRectF and now i would like to rotate it along.
    is there any way to rotate it easily and without use QPainter?

    I try this code:

    QRectF FirstRect = QRectF(0,0,100,50);
    QTransform t;
     t.rotate(45);
    QRectF SecondRect = t.mapToRect(FirstRect);
    

    I want to rotate the rectangle 45 degrees clockwise, but the SecondRect has a different origin and even the 4 corners position is not where they should be.
    For me it is important that the SecondRect is exactly the FirstRect rotated by 45 degrees.
    Did I get something wrong? I miss code?

    thank you

    J.HilkJ 1 Reply Last reply
    0
    • VRoninV Offline
      VRoninV Offline
      VRonin
      wrote on last edited by
      #2

      What's the centre of rotation supposed to be?

      "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
      ~Napoleon Bonaparte

      On a crusade to banish setIndexWidget() from the holy land of Qt

      1 Reply Last reply
      0
      • Gianluca86G Gianluca86

        Hi,
        I have a QRectF and now i would like to rotate it along.
        is there any way to rotate it easily and without use QPainter?

        I try this code:

        QRectF FirstRect = QRectF(0,0,100,50);
        QTransform t;
         t.rotate(45);
        QRectF SecondRect = t.mapToRect(FirstRect);
        

        I want to rotate the rectangle 45 degrees clockwise, but the SecondRect has a different origin and even the 4 corners position is not where they should be.
        For me it is important that the SecondRect is exactly the FirstRect rotated by 45 degrees.
        Did I get something wrong? I miss code?

        thank you

        J.HilkJ Offline
        J.HilkJ Offline
        J.Hilk
        Moderators
        wrote on last edited by
        #3

        @Gianluca86
        Well you need to adjust the offset than.

        //untested, out of my head ;-) I use something like this for a QPixmap rotation
        int iOffX = (FirstRect .width() - SecondRect .width())/2;
        int iOffY = (FirstRect .height()- SecondRect .height())/2;
        SecondRect.move(iOffX,iOffY);
        

        Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


        Q: What's that?
        A: It's blue light.
        Q: What does it do?
        A: It turns blue.

        1 Reply Last reply
        1
        • kshegunovK Offline
          kshegunovK Offline
          kshegunov
          Moderators
          wrote on last edited by kshegunov
          #4

          https://forum.qt.io/topic/78281/obb-oriented-bounding-box

          PS.
          In short, rotate the points, not the rectangle. The rectangle will always be a normal one - aligned to the axes.

          Read and abide by the Qt Code of Conduct

          1 Reply Last reply
          2
          • Gianluca86G Offline
            Gianluca86G Offline
            Gianluca86
            wrote on last edited by
            #5

            @VRonin : the center of rotation is the top left corner, which should be the default one, right?

            @kshegunov : so if I have to intersect two rectangles, one of which rotates, I need to rotate the points and create a polygon with the resulting 4 points?

            kshegunovK 1 Reply Last reply
            0
            • Gianluca86G Gianluca86

              @VRonin : the center of rotation is the top left corner, which should be the default one, right?

              @kshegunov : so if I have to intersect two rectangles, one of which rotates, I need to rotate the points and create a polygon with the resulting 4 points?

              kshegunovK Offline
              kshegunovK Offline
              kshegunov
              Moderators
              wrote on last edited by
              #6

              Or create a polygon from the points and then rotate it, but yes you do.

              Read and abide by the Qt Code of Conduct

              1 Reply Last reply
              1
              • Gianluca86G Offline
                Gianluca86G Offline
                Gianluca86
                wrote on last edited by
                #7

                Thanks for the explanation, now I try.

                1 Reply Last reply
                1
                • Gianluca86G Offline
                  Gianluca86G Offline
                  Gianluca86
                  wrote on last edited by
                  #8

                  I try to do this:

                  QTransform t;
                   t.rotate(45);
                  QPolygon Second = t.mapToPolygon(QRect(0,0,100,50));
                  

                  And the result is ok but with this code

                  QTransform t;
                   t.rotate(45);
                  QPolygon Second = t.mapToPolygon(QRect(10,10,100,50));
                  

                  the origin point (first point of polygon() changed

                  Did I misunderstand your explanation?

                  1 Reply Last reply
                  0
                  • Gianluca86G Offline
                    Gianluca86G Offline
                    Gianluca86
                    wrote on last edited by
                    #9

                    I don't why, but with this is ok:

                    QTransform t;
                    t.translate(10,10);
                    t.rotate(45);
                    t.translate(-10,-10);
                    QPolygon Second = t.mapToPolygon(QRect(10,10,100,50));
                    
                    kshegunovK 1 Reply Last reply
                    0
                    • Gianluca86G Gianluca86

                      I don't why, but with this is ok:

                      QTransform t;
                      t.translate(10,10);
                      t.rotate(45);
                      t.translate(-10,-10);
                      QPolygon Second = t.mapToPolygon(QRect(10,10,100,50));
                      
                      kshegunovK Offline
                      kshegunovK Offline
                      kshegunov
                      Moderators
                      wrote on last edited by
                      #10

                      That is because rotations are done around the origin of the coordinate system. Whenever a rotation should be done around another axis it is necessary to move the origin of the coordinate system to that axis, perform the rotation and then return the origin to its former position. This is basic linear algebra, about which you can read more here (and on its related pages).

                      Read and abide by the Qt Code of Conduct

                      1 Reply Last reply
                      2

                      • Login

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