Image rotation - getting the best result
-
Hello all,
I'm using Qt Creator 4.11.0 Based on Qt 5.12.8 (GCC 9.3.0, 64 bit). My development at the moment is on a desktop machine but eventually it'll be moved to an embedded linux device.
I have a label (circleLable) that contains a circular image (that is not smooth, i.e. irregular in parts of the circumference) that I'm trying to continuously & smoothly rotate on a stacked widget. The code I'm using to do this is:
void MainWindow::rotateImage(){ static int degrees = 0; degrees +=1; QPixmap pixmap(":/images/cicularImage.png"); QMatrix rm; rm.rotate(degrees); pixmap = pixmap.transformed(rm); ui->circleLabel->setPixmap(pixmap); // Code below will be moved elsewhere to only invoke once ui->circleLabel->setScaledContents( true ); ui->circleLabel->setSizePolicy( QSizePolicy::Ignored, QSizePolicy::Ignored ); }
While the image is rotating initial results leave something to be desired. I'm going to continuing working on it to improve results but can anyone share the experience of how to get the best in this situation?
A circular border around the image?
Does the image file format have any bearing on the quality of the image rotated?
Should I try an image without a transparent/ no background (if that is even possible)?
Is embedding a video the best way to go to get smooth results?Thanks for looking and any help,
-
Well, I've had some success.
The image rotation is working OK, smooth enough. I used an online tool to crop the image in a circular manner.
The issue is that the image is moving left & right within the Qlabel box/ frame. I have made the Qlabel the same size as the image but looks like there's more going on.
From the documentation & searching it looks like 0,0 for an image is top left hand corner, so I applied the transform as follows without success:
QTransform tr; tr.translate(width()/2, height()/2); tr.rotate(degrees); tr.translate(-width()/2, -height()/2); pixmap = pixmap.transformed(tr);
-
Image movement/wobble solved using:
ui->labelName->setAlignment(Qt::AlignHCenter | Qt::AlignVCenter);
Also for me it turns out translation was unnecessary. So this gave satisfactory results (called every 10msec):
void MainWindow::rotateImage(){ static int degrees = 0; degrees +=1; if (degrees > 359) degrees = 0; QPixmap pixmap(":/images/circularCroppedImage.png"); QTransform tr; tr.rotate(degrees); pixmap = pixmap.transformed(tr); ui->labelName->setPixmap(pixmap); }
I had wanted to display text on top of the image but it seems its not possible (as least not easy) to display both a pixmap & text so I just placed another label on top.
Hope this helps someone else out there!