如何在应用程序本身(Qt)内检测Qt GUI应用程序是否已空闲?

use*_*285 8 qt

如何在一段时间内检测GUI应用程序何时处于空闲状态(即无用户交互)?

我有n个Qt屏幕,我想在应用程序空闲5秒时带上Date-Time屏幕,当我点击Date-Time屏幕时它应该返回到最后一个屏幕.

目前我正在使用下面的代码,现在如何检查系统是否空闲5秒钟带来一个新表格,当一些身体鼠标移动/点击它应该回到最后一个表格.

CustomEventFilter::CustomEventFilter(QObject *parent) :
    QObject(parent)
{   
    m_timer.setInterval(5000);
    connect(&m_timer,SIGNAL(timeout()),this,SLOT(ResetTimer()));
}

bool CustomEventFilter::eventFilter(QObject *obj, QEvent *ev)
{
    if(ev->type() == QEvent::KeyPress ||
           ev->type() == QEvent::MouseMove)
    {
        ResetTimer();
        return QObject::eventFilter(obj, ev);

     }
    else
    {

    }
}

bool CustomEventFilter::ResetTimer()
{
    m_timer.stop(); // reset timer

}
Run Code Online (Sandbox Code Playgroud)

而我的main.cpp看起来像这样:

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    MainForm w;
    w.show();
    CustomEventFilter filter;
    a.installEventFilter(&filter);

    return a.exec();


}
Run Code Online (Sandbox Code Playgroud)

谢谢.

O.C*_*.C. 6

根据QT文档:

要使应用程序执行空闲处理(即,在没有挂起事件时执行特殊功能),请使用0超时的QTimer.使用processEvents()可以实现更高级的空闲处理方案.

因此,您需要创建一个零超时间隔的QTimer,并将其连接到应用程序空闲时调用的插槽.


Mac*_*cke 5

重写QCoreApplication :: notify和鼠标/键盘事件计时器?

(或者只是存储事件的时间,并让计时器定期检查该值,这可能比一直重置计时器要快。)

class QMyApplication : public QApplication
{
public:
    QTimer m_timer;

    QMyApplication() {
        m_timer.setInterval(5000);
        connect(&m_timer, SIGNAL(timeout()), this, SLOT(app_idle_for_five_secs());
        m_timer.start();
    }
slots:
    bool app_idle_for_five_secs() {
        m_timer.stop();
        // ...
    }
protected:
    bool QMyApplication::notify ( QObject * receiver, QEvent * event )
    {
        if (event->type == QEvent::MouseMove || event->type == QEvent::KeyPress) {
             m_timer.stop(); // reset timer
             m_timer.start();
        }    
        return QApplicaiton::notify(receiver, event);
    }
};
Run Code Online (Sandbox Code Playgroud)