簡體   English   中英

如何在代碼的兩個部分之間留出時間間隔或延遲同時程序繼續? C ++

[英]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線程,因此它會凍結。 嘗試使用singleShotQTimer 例如:

QTimer::singleShot(4000,this,SLOT(mySlot()));

mySlot您可以完成所有需要的工作。 在這種情況下, singleShot不會凍結您的GUI。 例如,將碰撞設置為零並調用更新,它將調用paintEvent並且在此paintEvent ,零碰撞將​​不會再次繪制橢圓。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM