[solved]Scaling and rotating QGraphicsPixmapItem



  • 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();
    

    }

    @


  • Lifetime Qt Champion

    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 ?



  • 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();
    

    }

    @


  • Lifetime Qt Champion

    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.



  • 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.


  • Lifetime Qt Champion

    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
    }
    @



  • 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();
    

    }

    @


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.