Important: Please read the Qt Code of Conduct -

Delay in QtWidget

  • Hi i am trying to write a simple code for drawPoint , but not able to get the delay. Here is simple code which i have written.

    include two header files

    void Mainwindow::delay(double sec)
    QTime dieTime= QTime::currentTime().addSecs(sec);
    while (QTime::currentTime() < dieTime)
    QCoreApplication::processEvents(QEventLoop::AllEvents, 100);

    calling delay using : delay(n) //n is seconds..

    Not able to get delay..

  • hi,

    QTimer::singleShot(100, [=]{ foo(); });

    is a possible solution ... but if your intent is to refresh sreen every 100ms I think the better use is QThread with :


    at the end of loop in Run call....


  • Thank for the reply..

    I want to create simple animation in Qt for my students. I have used this->msleep(100); , but I am geeting error"Program has terminated unexpectedly".

    I just want draw a point in for loop with some delay in order to show animation.

  • Lifetime Qt Champion

Log in to reply