Repaint QWidget using QTimer signal causes high CPU usage on Mac OS X



  • Hi,

    I use a QTimer timeout signal to repaint QWidget and so animate a sprite composed of some QImage loaded in QWidget constructor. My app works well on Windows XP and Windows 7. My CPU usage on a PC is about nil however in Mac OS X 10.7 and 10.8 it is greater than 70%. See below my source code :

    @
    MySprite::MySprite(QWidget *parent) :
    QWidget(parent)
    {
    timer = new QTimer(this);
    connect(timer, SIGNAL(timeout()), this, SLOT(repaint()));
    currentImage = 0;

    for(int i = 0;i<50;i++){
        image[i] = QImage(QString(":/gui/images/pulse/image%1.png").arg(i));
    }
    

    }

    void MySprite::paintEvent ( QPaintEvent * pe )
    {
    QPainter painter(this);
    painter.drawImage(0, 0, image[currentImage]);

    currentImage = (currentImage+1) % 50;
    

    }
    @

    I use QT 4.7.4.
    When I comment the paintEvent function content's, it's the same.

    Thank's for reading,
    Florian.


  • Lifetime Qt Champion

    Hi and welcome to devnet,

    Since you're not setting an interval it goes as fast as it can. Is it better if rather than repaint you connect to update or set interval to something sensible like 40ms ?



  • Sorry I forget a part of code that sets the timer interval, in my opinion that's not the problem. I check it anyway tomorow.

    Edit : I test with a fixed interval, like 20ms - my interval could be between 5 and 25ms - and regarding the CPU usage it's the same.


Log in to reply
 

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