Repainting screen every few minutes without crashing - C ++

4

I have the following situation:

I have to repaint the screen every few minutes so that objects on the screen get stuck on account, but this has to happen in a way that the program will not crash.

I have tried to make a Thread and call the update method but the screen turns white, all locked.

Basically, I need this to occur at a specific time, but not to influence the program's subsequent processes (such as keyboard and mouse events).

void atualizar(){
   X = X - 20;
   repaint();
}

I think the best thing is with Thread, but I could not implement it correctly, even following the official QT documentation.

Another point, when trying to do with Thread the painting event ends up being called twice, occurring in crash in the program.

    
asked by anonymous 08.06.2014 / 21:28

1 answer

4

One solution would be to use a QTimer :

QTimer *timer = new QTimer(this);
connect(timer, SIGNAL(timeout()), this, SLOT(repaint()));
timer->setInterval(200); // 200 milissegundos
timer->start(); // Se preferir, pode usar start(200) e remover a linha do setInterval

Remember that all GUI operations must be done on the main thread, and when you need to use other threads, the communication must be done by SIGNALS and SLOTS, never calling the methods directly between threads.

When you need to disable the timer, just call timer->stop(); and to reactivate, just call new timer->start();

See the Qt. manual for more details. p>     

08.06.2014 / 22:50