2 seconds after the first picture, the second should appear. How to make a program pause? I tried a lot of things. Either the program hangs, or simply does not work.

void Widget::paintEvent(QPaintEvent *event) { Q_UNUSED(event); QPainter painter(this); painter.setPen(QPen(Qt::black, 1, Qt::SolidLine, Qt::FlatCap)); if(ui->startButton->isChecked()){ painter.setBrush(QBrush(Qt::red, Qt::SolidPattern)); painter.drawRect(20,30, 180, 120); // здесь должна быть пауза painter.setBrush(QBrush(Qt::green, Qt::SolidPattern)); painter.drawEllipse(100, 150, 180, 200); } else if(ui->pauseButton->isChecked()){ } else if(ui->cleanButton->isChecked()){ } } void Widget::on_startButton_clicked() { repaint(); } void Widget::on_pauseButton_clicked() { repaint(); } void Widget::on_cleanButton_clicked() { repaint(); } 

    3 answers 3

    You can fall asleep in three ways with the help of static QThread functions:

     void QThread::msleep(unsigned long msecs); void QThread::sleep (unsigned long secs); void QThread::usleep(unsigned long usecs); 

    But in your case, this is not the best idea. Because the drawing is done in the main thread of the application and while it is sleeping, event handling, messages, etc. in this thread will not be executed. That is, in fact, the entire application will stand for 3 seconds, and this is a bit too much.

    Timers are better suited for your task. Qt has a very handy QTimer . The second option is if you need to do something at regular intervals at certain intervals, then you can use "built-in" timers in QObject . These timers are created / deleted using the functions startTimer / killTimer , processed in the virtual function QObject::timerEvent .

    And so that it does not hang for redrawing, use update() instead of repaint() .

      In fact, if you want to pause in a quick and simple way, it is better to use not sleep , but

      void QTest :: qWait (int ms) [static]

      Waiting for ms milliseconds. While there is a wait, events will be processed and your test continues to respond to events on the user interface and communication networks.

      And even more correctly to use, for example, QEventLoop , from the exec() cycle of which you can fall out on the required event (say, a one-time timer).

        If you have Qt5, you can do this:

         QThread::sleep(2); 

        In Qt4, this method is protected, so you have to dodge a little:

         void sleep(int ms){ static QMutex mutex; static QMutexLocker locker(&mutex); mutex.tryLock(ms); } //... sleep(2000); 

        But keep in mind these functions suspend the flow. If you call them in the main thread, the program will hang for 2 seconds.

        To do something after a certain period of time and not suspend the flow, use the timer:

         QTimer::singleShot(2000, object, SLOT(slot())); 
        • The problem is that the program crashes without displaying even the first drawing - kotelliada
        • @kotelliada, try to write QCoreApplication::processEvents(); before a pause QCoreApplication::processEvents(); - yrHeTaTeJlb