QML - Seeking best method to detect user inactivity.
-
wrote on 1 Nov 2021, 21:51 last edited by
Developing a medical device. My responsibility is to create the UI using QML. If a dialog/settings screen is presented, we want to dismiss it programmatically if there is no user interaction after about 60 seconds. Seems the QML EventFilter is not the same as that available in Qt/C++. If that was the case I could easily create a filter and check for activity, passing along any events that should occur to be processed normally. But the QML EventFilter appears to be restricted to web development and server events, of no interest to our application. (Not looking to do anything with the events at this time, or likely anything in the future except maybe to log that something has happened.)
Ideally, I a seeking a method that I could include in the 2 base classes for which it would be relevant and dismiss any children if the time allotted expires, or reset the timer if there is activity.
Thanks in advance for any suggestions.
-
Developing a medical device. My responsibility is to create the UI using QML. If a dialog/settings screen is presented, we want to dismiss it programmatically if there is no user interaction after about 60 seconds. Seems the QML EventFilter is not the same as that available in Qt/C++. If that was the case I could easily create a filter and check for activity, passing along any events that should occur to be processed normally. But the QML EventFilter appears to be restricted to web development and server events, of no interest to our application. (Not looking to do anything with the events at this time, or likely anything in the future except maybe to log that something has happened.)
Ideally, I a seeking a method that I could include in the 2 base classes for which it would be relevant and dismiss any children if the time allotted expires, or reset the timer if there is activity.
Thanks in advance for any suggestions.
@VFCraig As far as I know, you can install an event filter on your QApllication (in main.cpp) that should get all input activities independently of it being a QWidget or Qml application
-
wrote on 2 Nov 2021, 14:39 last edited by
Installed an event filter in the QApplication in main.cpp. Monitoring QEvent::InputMethod. Need to send a signal to my QML objects to know that the user is still active. Obviously I am not going to create an instance of QApplication in my QML code, how do I connect to a signal sent from the event filter in QML? (Every example I see involves instantiating an object to do so.)
I would suspect that QApplication may be exposed to QML in some manner?
-
Create an ActivityWatcher QObject class with an active property.
Use it as a an event filter for your QGuiApplication (qGuiApp->installEventFilter(this)
).
Register it as a singleton type to QML. -
wrote on 2 Nov 2021, 17:49 last edited by
@VFCraig said in QML - Seeking best method to detect user inactivity.:
Installed an event filter in the QApplication in main.cpp. Monitoring QEvent::InputMethod.
I'm not 100% certain, but I fear that
QEvent::InputMethod
is not going to be the best type to listen for. The name is (possibly) misleading. I think it pertains strictly toIME
interaction, such as what is described at: https://www.kdab.com/qt-input-method-depth/I think that unless the target computer (and/or target user) is using an IME for entering multibyte characters (Chinese, Japanese, Korean, Indian, etc), I think you may end up never seeing even a single
QEvent::InputMethod
arrive through your filter.You might also try:
QEvent::MouseButtonPress, QEvent::MouseButtonRelease, QEvent::KeyPress, QEvent::KeyRelease,
-
@VFCraig said in QML - Seeking best method to detect user inactivity.:
Installed an event filter in the QApplication in main.cpp. Monitoring QEvent::InputMethod.
I'm not 100% certain, but I fear that
QEvent::InputMethod
is not going to be the best type to listen for. The name is (possibly) misleading. I think it pertains strictly toIME
interaction, such as what is described at: https://www.kdab.com/qt-input-method-depth/I think that unless the target computer (and/or target user) is using an IME for entering multibyte characters (Chinese, Japanese, Korean, Indian, etc), I think you may end up never seeing even a single
QEvent::InputMethod
arrive through your filter.You might also try:
QEvent::MouseButtonPress, QEvent::MouseButtonRelease, QEvent::KeyPress, QEvent::KeyRelease,
wrote on 2 Nov 2021, 19:13 last edited by@KH-219Design I wouldn't be surprised if that is the case. Sure, mouse events, keyboard events, tablet events and wheel events inherit from input events, but why not use a name that sounds like it means the same for something completely different? Just like QML's version of an event filter, nothing to do with the event filter in C++.
-
Create an ActivityWatcher QObject class with an active property.
Use it as a an event filter for your QGuiApplication (qGuiApp->installEventFilter(this)
).
Register it as a singleton type to QML. -
wrote on 3 Nov 2021, 14:16 last edited by
@VFCraig said in QML - Seeking best method to detect user inactivity.:
Nothing in the QML documentation mentions an, "ActivityWatcher".
You will have to write a QObject based class in c++. Then register it to accept events from the gui event system. Then you can filter those events and fire a signal.
Registering it as singleton is just making sure there is only one created.
In QML you can bind a function to the signal to do what you want.
-
@GrecKo Nothing in the QML documentation mentions an, "ActivityWatcher". If this is some 3rd party addition, it is not relevant to this question.
@VFCraig That was meant as the name of the class you would need to create to watch the events of your application.
1/9