Not able to rotate a QPixmap



  • Not able to rotate a picture 360 degree, smoothly.
    page5_label= new QLabel();
    page5_label->setPixmap(QPixmap(":/cartopView.png"));

                 Layout_page5 = new QVBoxLayout(this);
                 Layout_page5->addWidget(page5_label);
                 QTimer::singleShot(60000,this,&MainWindow::pictureRotate);
    
    
      void MainWindow::pictureRotate()
    

    {
    while(1){

                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);
         }
    }

  • Moderators

    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 processEvents hack 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.


Log in to reply
 

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