[Qt-interest] Does QCoreApplication::processEvents() update timers?

Karol Krizka kkrizka at gmail.com
Tue Apr 28 21:53:47 CEST 2009


On Monday 27 April 2009 20:51:47 Girish Ramakrishnan wrote:
> Karol Krizka wrote:
> > Hi all,
> >
> > I am using a third party 3D engine to draw a scene in a QGLWidget. The
> > scene is quite big, so it takes a while to load, so I want to show some
> > kind of an animation while it loads. However, the engine is not thread
> > safe, so I cannot do the loading in one thread and drawing in another.
> > What I would like to do instead is to redrew the scene after each major
> > component has been loaded. It will be a bit choppy, but better than
> > nothing.
> >
> > I'm trying to accomplish this by having my drawing done inside a timer
> > loop and call QApplication::processEvents(). Basically, I started a timer
> > with QObject::startTimer() inside the QGLWidget and inside it I call
> > updateGL(). I'm hoping that when I call processEvents(), it will call
> > timerEvent() every while in order to trigger the redraw. However that
> > does not happen. The timerEvent() is not called at all during the loading
> > stage.
> >
> > So my question is, does QApplication::processEvents() also update any
> > running timers?
>
> Yes, your object will get timerEvent() if you do processEvents(). Maybe
> you can show some code with which you are facing this problem?
>
Take the following example. It performs some long action (printing out a lot 
of numbers) that is started from the first timer event. This is similiar to 
what I'm doing, but instead of printing numbers I'm loading textures.

#ifndef TIMERAPP_H
#define TIMERAPP_H

#include <QCoreApplication>

class TimerApp : public QCoreApplication
{
public:
    TimerApp(int argc,char* argv[]);

    void timerEvent(QTimerEvent *event);
    void performLongAction();

    bool firstGo;
};

#endif // TIMERAPP_H


#include "timerapp.h"

#include <QDebug>

TimerApp::TimerApp(int argc,char* argv[])
    :QCoreApplication(argc,argv),firstGo(true)
{
    startTimer(10);
}

void TimerApp::timerEvent(QTimerEvent *event)
{
    if(firstGo)
    {
        performLongAction();
        firstGo=false;
    }
    qDebug() << "Timer Event";
}

void TimerApp::performLongAction()
{
    for(int i=0;i<1000000;i++)
    {
        qDebug() << i;
        QCoreApplication::processEvents();
    }
}

#include "timerapp.h"

int main(int argc, char *argv[])
{
    TimerApp a(argc, argv);

    return a.exec();
}



> I also don't understand why you need to call processEvents() yourself.
> Why not just start the timer and run the event loop i.e qApp->exec()?
>
I do use qApp->exec(), but I have a long function (see my example above) 
that blocks qApp->exec(). so in order for my application to remain 
responsive I have to process the events via processEvents.
> Girish




More information about the Qt-interest-old mailing list