简体   繁体   中英

QTimer timeout signal doesn't emit

I'm using QTimer and timeout signal doesn't emit.

Here is my code:

    QTimer* receiveTimer = new QTimer();
    receiveTimer->setInterval(1000);
    connect(receiveTimer, SIGNAL(timeout()), this, SLOT(timeout()));

    QByteArray res;
    inWait = true;
    receiveTimer->start();

    qDebug() << "Before while";

    while (!res.contains("OK\r\n") && inWait)
        qDebug() << res.append(sPort->read(1));

    qDebug() << "After while";

    receiveTimer->stop();

At first, i had defined the timer at the class constructor, but it didn't worked either.

You could add this to your loop:

QCoreApplication::processEvents(QEventLoop::AllEvents, 100);

or

QApplication::processEvents(QEventLoop::AllEvents, 100);

My "sleep" function in qt:

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

}

If I anderstand you right, you want than something like that:

QByteArray res;

QTime dieTime= QTime::currentTime().addSecs(sec);
while (!res.contains("OK\r\n") && QTime::currentTime() < dieTime) {
    QCoreApplication::processEvents(QEventLoop::AllEvents, 100);
    qDebug() << res.append(sPort->read(1));
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM