How to time QPixmap::scaled?



  • Hello,

    Is there an easy way to check/print how long QPixmap::scaled scaling with painting took?


  • Moderators



  • @raven-worx QElapsedTimer on it's own does not give any meaningful results because of Qts Event system. I can clearly see that scaling takes longer than 1 ms but printing QElapsedTimer shows 0.


  • Moderators

    @Eligijus
    can you please show how you used it.



  • @raven-worx

    label->setPixmap(myPixmap);
    label->setScaledContents(true);
    QElapsedTimer timer;
    timer.start();
    label->resize(size);
    qDebug() << timer.elapsed();
    


  • @Eligijus
    I would do that slightly differen.

    //QElapsed timer as member and reimplementing a virtual function
    //In QLabel subclass
    public:
    void startTimer(){eTimer.start();}
    
    protected:
    QElapsedTimer eTimer;
    
    virtual void resizeEvent(QResizeEvent * event){
       Q_Unused(event);
       qDebug() << eTimer.elapsed();
    }
    
    //in your cpp
    label->setPixmap(myPixmap);
    label->setScaledContents(true);
    label->startTimer();
    label->resize(size);
    

    resizeEvent is called after the Widget has the new size e.g it should be painted than.


  • Moderators

    @Eligijus said in How to time QPixmap::scaled?:

    @raven-worx

    label->setPixmap(myPixmap);
    label->setScaledContents(true);
    QElapsedTimer timer;
    timer.start();
    label->resize(size);
    qDebug() << timer.elapsed();
    

    exactly this won't work, because of the resize and paint event being executed in the next event loop iteration (after your timer.elapsed() call).

    Try the following to force a paint:

    label->setPixmap(myPixmap);
    label->setScaledContents(true);
    QElapsedTimer timer;
    timer.start();
    label->resize(size);
    label->repaint();
    qDebug() << timer.elapsed();
    

    Alternatively sub-class QLabel and simply override the paintEvent() like this:

    void MyLabel::paintEvent(QPaintEvent* event)
    {
       QElapsedTimer timer;
       timer.start();
       QLabel::paintEvent(event);
       qDebug() << timer.elapsed();
    }
    


  • Thanks sub-classing and overrinding paintevent method worked.


Log in to reply
 

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