How do I make a time span or delay between two pieces of code while the program continues? C ++
I have the following code:
if(collision == 1)
{
painter->setBrush(QColor(Qt::red));
painter->setPen(QColor(Qt::black));
painter->drawEllipse(QPoint(boundingRect().x() + (boundingRect().width() / 1.7),
boundingRect().y() + (boundingRect().width() / 2.1)),
boundingRect().width() / 5,
boundingRect().height() / 10);
/*THERE SHOUD BE THE TIME GAP AND THEN DO*/
collision = 0;
}
I want to use this code to draw a red ellipse, but only for a few seconds (after collision). Therefore, I have to make a time span or delay between the two parts of this code. The problem here is that I don't know how to do it.
I tried sleep () or wait () or:
QTime dieTime= QTime::currentTime().addSecs(1);
while( QTime::currentTime() < dieTime )
QCoreApplication::processEvents(QEventLoop::AllEvents, 100);
but this STOP or PAUSE is a whole PROGRAM I just want to delay the execution of "collision = 0" Any ideas?
source to share
sleep()
or wait()
stop the entire GUI thread so that it hangs. Try using singleShot
from QTimer
. For example:
QTimer::singleShot(4000,this,SLOT(mySlot()));
As mySlot
you can do everything you need. In this case, singleShot
your graphical interface will not slow down. For example, set collision to zero and call update, it will trigger paintEvent
and in this paintEvent
zero collision your ellipse will not be drawn again.
source to share