0

hello all, I am trying to rotate the images in Qt. While rotating again and again, the size of the image decreases. how can i stop this size decrement ? please help me out. I am using this code.

void MyWidget::rotateLabel()
{
    QPixmap pixmap(*my_label->pixmap());
    QMatrix rm;
    rm.rotate(90);
    pixmap = pixmap.transformed(rm);
    my_label->setPixmap(pixmap);
}
Ashish
  • 219
  • 2
  • 6
  • 22

2 Answers2

1

I had to solve this problem earlier today. First, make sure that your QLabel widget does not have the scaledContents flag set to True; second, your image needs to be scaled to fit within the boundaries of the QLabel when rotated at 45 degrees; basically, you need to scale it to dimension/sqrt(2). Then, and only then, can you transform the image with your rotation.

I'm working in PyQt but the same principles apply.

    label = my_QLabel_instance
    angle = my_arbitrary_angle
    image = some_stored_reference_to_original_QImage_instance
    dimension = dimension / sqrt(2)

    transform = QtGui.QTransform().rotate(angle)
    pixmap = QtGui.QPixmap(image)
    pixmap = pixmap.scaled(dimension, dimension)
    pixmap = pixmap.transformed(transform, QtCore.Qt.SmoothTransformation)

    label.setPixmap(pixmap)
Kyle
  • 322
  • 2
  • 7
0

In the Qt help files for QPixmap::transformed, it states: -

The transformation transform is internally adjusted to compensate for unwanted translation; i.e. the pixmap produced is the smallest pixmap that contains all the transformed points of the original pixmap. Use the trueMatrix() function to retrieve the actual matrix used for transforming the pixmap.

I assume that the width and height of your image are not equal. Therefore, as you've observed, each rotation will reduce the size, to ensure the full image is maintained.

Change the original image to have equal width and height by adding a border, or scale the image before or after the rotation, but image quality will suffer.

Alternatively, if you're always rotating 90 degrees, you could rotate the images in an external art program, load all 4 images and just set which one you want, rather than rotating them via Qt.

============== EDITED ======================

In response to the comments of having varying rotations, you need to ensure that you have a source image that is never rotated, then for each iteration, create an image from the source image that you need to prevent both reduction in image size and image quality.

In pseudo code it would be something like this: -

Image srcImage(":/myimage");

void MyWidget::rotateLabel()
{
    Image img(srcImage);
    img.rotate(rotation);
    label->setPixmap(img);
}
TheDarkKnight
  • 27,181
  • 6
  • 55
  • 85
  • Thnks for your help Merlin, i have already tried to resize the image after rotation but the size of image still decreases.please tell me something else or anyone else who can help me out of this. – Ashish Jun 20 '13 at 06:10
  • Are you always rotating 90 degrees? – TheDarkKnight Jun 20 '13 at 07:36
  • No Merlin, its not always 90 degrees , it could be 25,35,37,128,230 means its not a fixed value. – Ashish Jun 26 '13 at 05:28