how to detect idle status
-
-
Hi @Solayer,
You have to install an event filter in your application, which will restart a timer of the desired timeout each time an user interaction event is received, when timer goes off, it's mean your application is in a "idle status".
How to create an event filter?
https://doc.qt.io/qt-5/qobject.html#installEventFilterHow wait for a desired time in QObject?
https://doc.qt.io/qt-5/qobject.html#startTimer
or use a QTimer.How to install an event filter in QApplication?
QApplication app(argc, argv); app.installEventFilter(myEventFilter);
Regards,
-
@Johan_R28 is absolutely right, an eventFilter is the way to go.
But because I know how difficult it is to warp one own head around the concept,
here's a very simple example
https://github.com/DeiVadder/Topic112043 -
hello. it's me again.
i have a question to QTimer.
when i write like this:fileio.cpp
FileIO::FileIO(QObject *parent) : QObject(parent){ m_timer.setInterval(5000); connect(&m_timer,&QTimer::timeout, &m_timer, &QTimer::stop); connect(&m_timer,&QTimer::timeout,this,[=](){this->goLockScreen();}); m_timer.start(); } bool FileIO::eventFilter(QObject *obj, QEvent *event){ if (event->type() == QEvent::MouseMove){ m_timer.start(); } return QObject::eventFilter(obj, event); } void FileIO::goLockScreen(){ emit emitTimeOut(); // m_timer.stop(); qDebug()<<"test C"; }
main.qml
FileIO{ id: file onEmitTimeOut: { console.log("QML") } }
output when i don't anything
test C test C qml: QML test C test C test C test C
but i when i write like this
fileio.cppFileIO::FileIO(QObject *parent) : QObject(parent){ m_timer.setInterval(5000); connect(&m_timer,&QTimer::timeout,this,[=](){this->goLockScreen();}); } bool FileIO::eventFilter(QObject *obj, QEvent *event){ if (event->type() == QEvent::MouseMove){ m_timer.start(); } return QObject::eventFilter(obj, event); } void FileIO::goLockScreen(){ emit emitTimeOut(); m_timer.stop(); qDebug()<<"test C"; }
main.qml
FileIO{ id: file onEmitTimeOut: { console.log("QML") } }
output when i move in app, console.log in qml not work
test C
i don't understang what happend, why when i move mouse in app then m_timer start until timeout then emit signal but qml don't receive.
m_timer.start in same scope then working fine.
sorry my english very bad.
thank you very much @J-Hilk @Johan_R28 when you helped me. -
@Solayer The problem is (most likely), that your FileIO instance that you use as global event filter is not the same instance as the one in your QML file.
You have to connect the FileIO signal inside main with a signal inside your QML file.
I'll update the repo when I get the time.
-
@Solayer
patched, link still applies:
https://github.com/DeiVadder/Topic112043