[英]How to make time gap or delay between two parts of code meanwhile program continue? C++
我有以下代碼:
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;
}
我想用這個代碼來繪制紅色橢圓,但只有幾秒鍾(碰撞后)。 因此,我必須在此代碼的兩個部分之間留出時間間隔或延遲。 這里的問題是我不知道該怎么做。
我試過sleep()或者wait()或者:
QTime dieTime= QTime::currentTime().addSecs(1);
while( QTime::currentTime() < dieTime )
QCoreApplication::processEvents(QEventLoop::AllEvents, 100);
但是這些STOP或PAUSE整個程序我只想延遲執行“collision = 0”任何想法?
sleep()
或wait()
停止所有GUI線程,因此它會凍結。 嘗試使用singleShot
從QTimer
。 例如:
QTimer::singleShot(4000,this,SLOT(mySlot()));
在mySlot
您可以完成所有需要的工作。 在這種情況下, singleShot
不會凍結您的GUI。 例如,將碰撞設置為零並調用更新,它將調用paintEvent
並且在此paintEvent
,零碰撞將不會再次繪制橢圓。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.