如何在QT中睡眠/暂停? [英] How to sleep/pause in Qt?

查看:81
本文介绍了如何在QT中睡眠/暂停?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在Qt中"睡眠/暂停"。

我希望用户界面在代码休眠时保持响应。

while(Tablet.IsConnected() == false){
    LogText("[Prep] Tablet not turned back on... Retrying...");
    //Sleep for three seconds here
}
LogText("[Prep] Tablet Detected!");

推荐答案

让平板电脑发出信号:

您将在构造函数中执行以下操作:

connect(Tablet, SIGNAL(connected()), this, SLOT(onConnected());

然后在

中进行处理
slots:
void connected()
{
    LogText("[Prep] Tablet Detected!");
}

如果没有可用的信号(第三方库),则可以使用QTimer重复检查:

class MyClass:public QObject
{
    Q_OBJECT
    QTimer timer;
    Tablet tablet;
public:
    MyClass(QObject * parent = 0) : QObject(parent)
    {
        connect(&timer, SIGNAL(timeout()), SLOT(connected());
        timer.setSingleShot(false);
        timer.setInterval(3000);
        timer.start();
    }
    Q_SLOT void connected()
    {        
       if (!tablet.isConnected())
       {
         LogText("[Prep] Tablet not turned back on... Retrying...");
         return;//wait for next timeout from the timer
       }

       LogText("[Prep] Tablet Detected!");
       timer.stop();
       //do some processing 
    }
}

这篇关于如何在QT中睡眠/暂停?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆