Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct
Not able to rotate a QPixmap
houmingc last edited by houmingc
Not able to rotate a picture 360 degree, smoothly.
page5_label= new QLabel();
Layout_page5 = new QVBoxLayout(this); Layout_page5->addWidget(page5_label); QTimer::singleShot(60000,this,&MainWindow::pictureRotate); void MainWindow::pictureRotate()
QPixmap ship(":/cartopView.png"); QPixmap rotate(ship.size()); QPainter p(&rotate); p.setRenderHint(QPainter::Antialiasing); p.setRenderHint(QPainter::SmoothPixmapTransform); p.setRenderHint(QPainter::HighQualityAntialiasing); p.translate(rotate.size().width() / 2, rotate.size().height() / 2); p.rotate(counter); p.translate(-rotate.size().width() / 2, -rotate.size().height() / 2); p.drawPixmap(0,0,ship); p.end(); page5_label->setPixmap(rotate); } }
First of all
while(1)in a UI thread is a very bad idea. This effectively freezes your ui.
A quick fix is to add
qApp->processEvents();at the end of the loop, but that's really just a band-aid.
Instead of the loop I would suggest to start a timer at reasonable interval (not shorter than an average monitor can actually display). You wouldn't need the
processEventshack with that.
Another thing is that you're loading an image, setting painter hints and recreating two pixmaps and a painter every step of the animation. That's terribly inefficient. Take them all out of the loop and just redo the painting, not the whole shabang.