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]Scaling and rotating QGraphicsPixmapItem
QtWS25 Last Chance

[solved]Scaling and rotating QGraphicsPixmapItem

Scheduled Pinned Locked Moved General and Desktop
7 Posts 2 Posters 4.1k 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.
  • V Offline
    V Offline
    venkatesh
    wrote on 28 Sept 2013, 11:44 last edited by
    #1

    Hi,

    I am using QMatrix for scaling and rotating a QGraphicsPixmapItem and QTransform for flipping it. But i am currently having a problem, which is when i rotate a image and after that scale the image. The rotation gets reset to the initial position. Which means that i am unable to update the rotate transformation to the scaling. I am using @ QDial @ for rotating and @ QSlider @ for zooming respectively. I would like to update each transformation to zoom,scale, and flip. So that it doesn't get reset when changing another parameter. I would be honoured to have your guidance.

    My code

    @

    void ImageRegistration::on_ZoomImage_sliderMoved(int position)
    {
    QMatrix MovingImageScaleMatrix;
    ui->ZoomImage->setRange(1,8);
    MovingImageScaleMatrix.translate(MovingImagePixmap.size().width()/2,MovingImagePixmap.size().height()/2);
    MovingImageScaleMatrix.scale((position1.5)/1.5,(position1.5)/1.5);
    MovingImageScaleMatrix.translate(-MovingImagePixmap.width()/2,-MovingImagePixmap.size().height()/2);
    MovingImageItem->setTransformationMode(Qt::SmoothTransformation);
    MovingImageItem->setMatrix(MovingImageScaleMatrix);
    MovingImageItem->update();

     QMatrix DifferenceImageScaleMatrix;
     DifferenceImageScaleMatrix.translate(MovingvtkImagePixmap.size().width()/2,MovingvtkImagePixmap.size().height()/2);
     DifferenceImageScaleMatrix.scale((position*1.5)/1.5,(position*1.0)/1.5);
     DifferenceImageScaleMatrix.translate(-MovingvtkImagePixmap.width()/2,-MovingvtkImagePixmap.size().height()/2);
     MovingvtkImageItem->setTransformationMode(Qt::SmoothTransformation);
     MovingvtkImageItem->setMatrix(DifferenceImageScaleMatrix);
     MovingvtkImageItem->update();
    

    }
    void ImageRegistration::on_RotateImage_sliderMoved(int position)
    {
    QMatrix MovingImageRotateMatrix;
    MovingImageRotateMatrix.translate(MovingImagePixmap.size().width()/2,MovingImagePixmap.size().height()/2);
    MovingImageRotateMatrix.rotate(position/0.85);
    MovingImageRotateMatrix.translate(-MovingImagePixmap.size().width()/2,-MovingImagePixmap.size().height()/2);
    MovingImageItem->setMatrix(MovingImageRotateMatrix);
    MovingImageItem->update();

    QMatrix DifferenceImageRotateMatrix;
    DifferenceImageRotateMatrix.translate(MovingvtkImagePixmap.size().width()/2,MovingvtkImagePixmap.size().height()/2);
    DifferenceImageRotateMatrix.rotate(position/0.85);
    DifferenceImageRotateMatrix.translate(-MovingvtkImagePixmap.size().width()/2,-MovingvtkImagePixmap.size().height()/2);
    MovingvtkImageItem->setMatrix(DifferenceImageRotateMatrix);
    MovingvtkImageItem->update();
    

    }

    @

    1 Reply Last reply
    0
    • S Offline
      S Offline
      SGaist
      Lifetime Qt Champion
      wrote on 28 Sept 2013, 20:05 last edited by
      #2

      Hi,

      That's because you're creating a new matrix each time you call your functions. Either keep the matrix as a member variable or get the matrix from the item, update it and set it again.

      Since you're mentioning QMatrix and QTransform, why not use QTransform to do everything ?

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      0
      • V Offline
        V Offline
        venkatesh
        wrote on 30 Sept 2013, 11:11 last edited by
        #3

        Hi,

        I tried making the @MovingImageTransform@ and @DifferenceImageTransform@ as member function of the
        @ class ImageRegistration @

        By declaring it in the header file, but when i access it, it provides undesirable result. The slider no longer does zoom out. The same way the QDial doesn't rotate anti clockwise. I am not sure why does this happen

        @

        void ImageRegistration::on_ZoomImage_sliderMoved(int position)
        {
        ui->ZoomImage->setRange(1,8);
        MovingImageTransform.translate(MovingImagePixmap.size().width()/2,MovingImagePixmap.size().height()/2);
        MovingImageTransform.scale((position1.5)/1.5,(position1.5)/1.5);
        MovingImageTransform.translate(-MovingImagePixmap.width()/2,-MovingImagePixmap.size().height()/2);
        MovingImageItem->setTransformationMode(Qt::SmoothTransformation);
        MovingImageItem->setTransform(MovingImageTransform);
        MovingImageItem->update();

         DifferenceImageTransform.translate(MovingvtkImagePixmap.size().width()/2,MovingvtkImagePixmap.size().height()/2);
         DifferenceImageTransform.scale((position*1.5)/1.5,(position*1.0)/1.5);
         DifferenceImageTransform.translate(-MovingvtkImagePixmap.width()/2,-MovingvtkImagePixmap.size().height()/2);
         MovingvtkImageItem->setTransformationMode(Qt::SmoothTransformation);
         MovingvtkImageItem->setTransform(DifferenceImageTransform);
         MovingvtkImageItem->update();
        

        }
        void ImageRegistration::on_RotateImage_sliderMoved(int position)
        {
        QTransform MovingImageRotateTransform=MovingImageItem->transform();
        MovingImageItem->update();
        MovingImageRotateTransform.translate(MovingImagePixmap.size().width()/2,MovingImagePixmap.size().height()/2);
        MovingImageRotateTransform.rotate(position/0.85);
        MovingImageRotateTransform.translate(-MovingImagePixmap.size().width()/2,-MovingImagePixmap.size().height()/2);
        MovingImageItem->setTransform(MovingImageRotateTransform);
        MovingImageItem->update();

        QTransform DifferenceImageRotateTransform=MovingvtkImageItem->transform();
        MovingvtkImageItem->update();
        DifferenceImageRotateTransform.translate(MovingvtkImagePixmap.size().width()/2,MovingvtkImagePixmap.size().height()/2);
        DifferenceImageRotateTransform.rotate(position/0.85);
        DifferenceImageRotateTransform.translate(-MovingvtkImagePixmap.size().width()/2,-MovingvtkImagePixmap.size().height()/2);
        MovingvtkImageItem->setTransform(DifferenceImageRotateTransform);
        MovingvtkImageItem->update();
        

        }

        @

        1 Reply Last reply
        0
        • S Offline
          S Offline
          SGaist
          Lifetime Qt Champion
          wrote on 30 Sept 2013, 13:14 last edited by
          #4

          Because you are "adding" your current modification to your latest transform. But I may have misunderstood you and thus misguided you, sorry.

          I would recommend that you take a look again at how to setup a QTransform properly. The doc has a good example.

          You should also rather have only one parameterless slot that will get the value of your two controls and setup the transform the way you want it. Making it so, you will only have one place where you do that which is generally better than spreading it over several function.

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          0
          • V Offline
            V Offline
            venkatesh
            wrote on 30 Sept 2013, 13:33 last edited by
            #5

            Hi,

            But in my application, i am using slider to zoom the image and QDial to rotate. I am not sure how can i implement it as single function. I checked many examples. Most of it, use a single transform to do the rotation, and scaling. Its all done as a single function, i couldn't find a implementation where its done with two different ui components.

            1 Reply Last reply
            0
            • S Offline
              S Offline
              SGaist
              Lifetime Qt Champion
              wrote on 30 Sept 2013, 13:37 last edited by
              #6

              Just connect the valueChanged signal of both your widgets to one slot and in that slot retrieve the values of your controls:

              @
              void updateTransform()
              {
              int zoomValue = _myZoomSlider->value();
              int rotationValue = _myRotationDiag->value();
              // Insert code to setup the QTransform
              }
              @

              Interested in AI ? www.idiap.ch
              Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

              1 Reply Last reply
              0
              • V Offline
                V Offline
                venkatesh
                wrote on 30 Sept 2013, 14:57 last edited by
                #7

                Hi,

                Thanks a lot it works perfectly, my implementation

                @

                connect(ui>ZoomImage,SIGNAL(valueChanged(int)),
                this,SLOT(updateTransform()));
                connect(ui>RotateImage,SIGNAL(valueChanged(int)),
                this,SLOT(updateTransform()));

                @

                then

                @

                void ImageRegistration::updateTransform()
                {
                int zoomValue=ui->ZoomImage->value();
                int rotateValue=ui->RotateImage->value();

                QTransform Transform;
                
                ui->ZoomImage->setRange(1,8);
                
                Transform.translate(MovingImagePixmap.size().width()/2,MovingImagePixmap.size().height()/2);
                Transform.rotate(rotateValue*1);
                Transform.translate(-MovingImagePixmap.size().width()/2,-MovingImagePixmap.size().height()/2);
                MovingImageItem->setTransform(Transform);
                MovingImageItem->update();
                
                
                Transform.translate(MovingImagePixmap.size().width()/2,MovingImagePixmap.size().height()/2);
                Transform.scale((zoomValue*1.5)/1.5,(zoomValue*1.5)/1.5);
                Transform.translate(-MovingImagePixmap.width()/2,-MovingImagePixmap.size().height()/2);
                MovingImageItem->setTransformationMode(Qt::SmoothTransformation);
                MovingImageItem->setTransform(Transform);
                MovingImageItem->update();
                
                
                Transform.translate(MovingvtkImagePixmap.size().width()/2,MovingvtkImagePixmap.size().height()/2);
                Transform.rotate(rotateValue*1);
                Transform.translate(-MovingvtkImagePixmap.size().width()/2,-MovingvtkImagePixmap.size().height()/2);
                MovingvtkImageItem->setTransform(Transform);
                MovingvtkImageItem->update();
                
                
                Transform.translate(MovingvtkImagePixmap.size().width()/2,MovingvtkImagePixmap.size().height()/2);
                Transform.scale((zoomValue*1.05)/1.05,(zoomValue*1.05)/1.05);
                Transform.translate(-MovingvtkImagePixmap.width()/2,-MovingvtkImagePixmap.size().height()/2);
                MovingvtkImageItem->setTransformationMode(Qt::SmoothTransformation);
                MovingvtkImageItem->setTransform(Transform);
                MovingvtkImageItem->update();
                

                }

                @

                1 Reply Last reply
                0

                3/7

                30 Sept 2013, 11:11

                • Login

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