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 along center point
QtWS25 Last Chance

Rotate QRectF along center point

Scheduled Pinned Locked Moved Unsolved General and Desktop
13 Posts 3 Posters 13.8k Views
  • 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.
  • AlvaroSA Offline
    AlvaroSA Offline
    AlvaroS
    wrote on last edited by
    #1

    Hello to all.

    First of all thanks a lot for reading this post and being able to help.

    I have a QRectF and now i would like to rotate it along its center point giving an angle.
    is there any way to rotate it easyly or i have to calculate the new coordinates?

    Thanks a lot!

    1 Reply Last reply
    0
    • A Offline
      A Offline
      Asperamanca
      wrote on last edited by
      #2

      You'll probably want to use QTransform for this. Define the desired transformation, the use the "map" functions of QTransform.
      Depending on what exactly you want, you can either user mapRect or work with a QPolygonF instead of a QRectF.

      The easiest way is if the point of rotation is the origin (0/0). Otherwise, I have to first translate the point of rotation into the origin, then rotate, then translate in the inverse direction.

      AlvaroSA 1 Reply Last reply
      0
      • A Asperamanca

        You'll probably want to use QTransform for this. Define the desired transformation, the use the "map" functions of QTransform.
        Depending on what exactly you want, you can either user mapRect or work with a QPolygonF instead of a QRectF.

        The easiest way is if the point of rotation is the origin (0/0). Otherwise, I have to first translate the point of rotation into the origin, then rotate, then translate in the inverse direction.

        AlvaroSA Offline
        AlvaroSA Offline
        AlvaroS
        wrote on last edited by
        #3

        @Asperamanca First of all thanks a lot for replying!
        Coudl you show a little code please?

        I am going to check QTransform!

        Thanks again.

        raven-worxR 1 Reply Last reply
        0
        • AlvaroSA AlvaroS

          @Asperamanca First of all thanks a lot for replying!
          Coudl you show a little code please?

          I am going to check QTransform!

          Thanks again.

          raven-worxR Offline
          raven-worxR Offline
          raven-worx
          Moderators
          wrote on last edited by
          #4

          @AlvaroS

          qreal angle = ...;
          QRect rect = ...;
          Qpoint center = rect.center();
          QTransform t = QTransform().translate( center.x(), center-y() ).rotate( angle ).translate( -center.x(), -center.y() );
          QPolygon rotatedRect =  t.mapToPolygon( rect );  // mapRect() returns the bounding rect of the rotated rect
          

          alternatively you can transform each point of the rect individually.

          --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
          If you have a question please use the forum so others can benefit from the solution in the future

          AlvaroSA 2 Replies Last reply
          1
          • raven-worxR raven-worx

            @AlvaroS

            qreal angle = ...;
            QRect rect = ...;
            Qpoint center = rect.center();
            QTransform t = QTransform().translate( center.x(), center-y() ).rotate( angle ).translate( -center.x(), -center.y() );
            QPolygon rotatedRect =  t.mapToPolygon( rect );  // mapRect() returns the bounding rect of the rotated rect
            

            alternatively you can transform each point of the rect individually.

            AlvaroSA Offline
            AlvaroSA Offline
            AlvaroS
            wrote on last edited by
            #5

            @raven-worx Thanks a lot for replying!

            Is there any way to convert from QPolygon to QRectF? Because I would like to add to a scene the rectange as QGprahicsRectItem.

            Thanks a lot!

            A raven-worxR 2 Replies Last reply
            0
            • raven-worxR raven-worx

              @AlvaroS

              qreal angle = ...;
              QRect rect = ...;
              Qpoint center = rect.center();
              QTransform t = QTransform().translate( center.x(), center-y() ).rotate( angle ).translate( -center.x(), -center.y() );
              QPolygon rotatedRect =  t.mapToPolygon( rect );  // mapRect() returns the bounding rect of the rotated rect
              

              alternatively you can transform each point of the rect individually.

              AlvaroSA Offline
              AlvaroSA Offline
              AlvaroS
              wrote on last edited by
              #6

              @raven-worx I have just done your code and it does not work. Rect translate but nor rotate!

              1 Reply Last reply
              0
              • AlvaroSA AlvaroS

                @raven-worx Thanks a lot for replying!

                Is there any way to convert from QPolygon to QRectF? Because I would like to add to a scene the rectange as QGprahicsRectItem.

                Thanks a lot!

                A Offline
                A Offline
                Asperamanca
                wrote on last edited by
                #7

                @AlvaroS said:

                @raven-worx Thanks a lot for replying!

                Is there any way to convert from QPolygon to QRectF? Because I would like to add to a scene the rectange as QGprahicsRectItem.

                Thanks a lot!

                Well, that changes a lot. Because in this case it is better to rotate the QGraphicsRectItem within the scene.

                Use setTransformOriginPoint to specify the point where the rect should rotate around.
                The use setRotation to rotate it.

                AlvaroSA 1 Reply Last reply
                1
                • A Asperamanca

                  @AlvaroS said:

                  @raven-worx Thanks a lot for replying!

                  Is there any way to convert from QPolygon to QRectF? Because I would like to add to a scene the rectange as QGprahicsRectItem.

                  Thanks a lot!

                  Well, that changes a lot. Because in this case it is better to rotate the QGraphicsRectItem within the scene.

                  Use setTransformOriginPoint to specify the point where the rect should rotate around.
                  The use setRotation to rotate it.

                  AlvaroSA Offline
                  AlvaroSA Offline
                  AlvaroS
                  wrote on last edited by
                  #8

                  @Asperamanca Thanks a lot!
                  It works but not at all.
                  My code is:

                  boxes_rotated->setTransformOriginPoint(center_box);
                  boxes_rotated->setRotation(-ui->Degrees_to_Rotate->text().toDouble());
                  

                  I mean, in scene Rectangle rotate really good but the coordinates points rectangle do not change... so they are the same rotated or not rotated. How can I fix that?

                  thanks a lot!

                  1 Reply Last reply
                  0
                  • AlvaroSA AlvaroS

                    @raven-worx Thanks a lot for replying!

                    Is there any way to convert from QPolygon to QRectF? Because I would like to add to a scene the rectange as QGprahicsRectItem.

                    Thanks a lot!

                    raven-worxR Offline
                    raven-worxR Offline
                    raven-worx
                    Moderators
                    wrote on last edited by
                    #9

                    @AlvaroS said:

                    Is there any way to convert from QPolygon to QRectF?

                    no, since it has a reason why a QPolygon is returned and not a QRect. A transformed rect isn't a "normal" rect anymore. In your case it still may arguable,. But imagine a transformation which is also shearing for example. Then a rect becomes an arbitrary shape.

                    Because I would like to add to a scene the rectange as QGprahicsRectItem.

                    why not adding the rect item and apply the transformation directly to it?

                    --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
                    If you have a question please use the forum so others can benefit from the solution in the future

                    AlvaroSA 1 Reply Last reply
                    0
                    • raven-worxR raven-worx

                      @AlvaroS said:

                      Is there any way to convert from QPolygon to QRectF?

                      no, since it has a reason why a QPolygon is returned and not a QRect. A transformed rect isn't a "normal" rect anymore. In your case it still may arguable,. But imagine a transformation which is also shearing for example. Then a rect becomes an arbitrary shape.

                      Because I would like to add to a scene the rectange as QGprahicsRectItem.

                      why not adding the rect item and apply the transformation directly to it?

                      AlvaroSA Offline
                      AlvaroSA Offline
                      AlvaroS
                      wrote on last edited by
                      #10

                      @raven-worx

                                  QRectF newbox = current_box->rect();
                                  
                                  QPointF center_box = current_box->rect().center();
                      
                                  std::cout<<"before: "<<newbox.topLeft().x()<<std::endl;
                                  current_box->setTransformOriginPoint(center_box);
                                  current_box->setRotation(-ui->Degrees_to_Rotate->text().toDouble());
                                  std::cout<<"after: "<<boxes_rotated->rect().topLeft().x()<<std::endl;
                      

                      current_box is a QGraphicsRectItem.

                      This box is which I want to rotate. So in the scene it is rotated good. But its points are still the same...

                      raven-worxR 1 Reply Last reply
                      0
                      • AlvaroSA AlvaroS

                        @raven-worx

                                    QRectF newbox = current_box->rect();
                                    
                                    QPointF center_box = current_box->rect().center();
                        
                                    std::cout<<"before: "<<newbox.topLeft().x()<<std::endl;
                                    current_box->setTransformOriginPoint(center_box);
                                    current_box->setRotation(-ui->Degrees_to_Rotate->text().toDouble());
                                    std::cout<<"after: "<<boxes_rotated->rect().topLeft().x()<<std::endl;
                        

                        current_box is a QGraphicsRectItem.

                        This box is which I want to rotate. So in the scene it is rotated good. But its points are still the same...

                        raven-worxR Offline
                        raven-worxR Offline
                        raven-worx
                        Moderators
                        wrote on last edited by
                        #11

                        @AlvaroS said:

                        This box is which I want to rotate. So in the scene it is rotated good. But its points are still the same...

                        because this only alters it's trasnformation. The item's rect is unchanged.
                        If you really want to change the coordinates of the item and display a rotated rect item you only can only use a polygon.

                        note: in a "normal" rect (per definition) the top-left and top-right coordinates have the same y-value, the top-right and the bottom-right the same x-value, and so on...

                        --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
                        If you have a question please use the forum so others can benefit from the solution in the future

                        AlvaroSA 1 Reply Last reply
                        0
                        • raven-worxR raven-worx

                          @AlvaroS said:

                          This box is which I want to rotate. So in the scene it is rotated good. But its points are still the same...

                          because this only alters it's trasnformation. The item's rect is unchanged.
                          If you really want to change the coordinates of the item and display a rotated rect item you only can only use a polygon.

                          note: in a "normal" rect (per definition) the top-left and top-right coordinates have the same y-value, the top-right and the bottom-right the same x-value, and so on...

                          AlvaroSA Offline
                          AlvaroSA Offline
                          AlvaroS
                          wrote on last edited by
                          #12

                          @raven-worx Thanks a lot!!

                          I have use what you said in your previous post like this:

                                      QRectF newbox = boxes_rotated->rect();
                          
                                      QTransform t = QTransform().translate( center_box.x(), center_box.y() ).rotate( -ui->Degrees_to_Rotate->text().toDouble() ).translate( -center_box.x(), -center_box.y() );
                                      QPolygon rotatedRect =  t.mapToPolygon( newbox.toRect() );  // mapRect() returns the bounding rect of the rotated rect
                          
                                      scene->addPolygon(rotatedRect,blue);
                          

                          but the problem is that in t.mapToPolygon I have to use QRect and QPolygon instead of QRectF and QPolygonF and I loose precision.

                          Is there any way to do that without loosing precision?!

                          thanks a lot again

                          raven-worxR 1 Reply Last reply
                          0
                          • AlvaroSA AlvaroS

                            @raven-worx Thanks a lot!!

                            I have use what you said in your previous post like this:

                                        QRectF newbox = boxes_rotated->rect();
                            
                                        QTransform t = QTransform().translate( center_box.x(), center_box.y() ).rotate( -ui->Degrees_to_Rotate->text().toDouble() ).translate( -center_box.x(), -center_box.y() );
                                        QPolygon rotatedRect =  t.mapToPolygon( newbox.toRect() );  // mapRect() returns the bounding rect of the rotated rect
                            
                                        scene->addPolygon(rotatedRect,blue);
                            

                            but the problem is that in t.mapToPolygon I have to use QRect and QPolygon instead of QRectF and QPolygonF and I loose precision.

                            Is there any way to do that without loosing precision?!

                            thanks a lot again

                            raven-worxR Offline
                            raven-worxR Offline
                            raven-worx
                            Moderators
                            wrote on last edited by raven-worx
                            #13

                            @AlvaroS said:

                            Is there any way to do that without loosing precision?!

                            Use one of the map() overloads
                            QPolygonF accepts a QRectF via constructor for example.

                            --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
                            If you have a question please use the forum so others can benefit from the solution in the future

                            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