qt - How can I reset a timer every time I receive a touch event from a qml page -
import qtquick 2.6; import qtquick.controls 2.1 ; import qtquick.layouts 1.3 ; page{ id: page width: 800 height: 1024 background: rectangle { color: "black" ; anchors.fill:parent ; } rectangle { id:rect1 x: 0 y:10 width: 100 height: 100 color : "red" mousearea { anchors.fill: parent onclicked: tmr.restart() } } rectangle { id:rect2 x: 0 y:110 width: 100 height: 100 color : "blue" mousearea { anchors.fill: parent onclicked: tmr.restart() } } timer { id : tmr interval : 30000 repeat : true running: true ontriggered: { console.log ("hello world ") } } }
i develop software embedded imx6 freescale device using qt framework.
basically want restart timer every time click , every time touch event on screen whether click/touch happen inside mouse area of rectangles or outside of them.
the idea similar screensaver.
there multiple ways, , right way depends on requirements.
if don't need guarantee timer triggers during input can layer mousearea
on top of everything. in mousearea
handle pressed
-signals, dont accept
them.
this allows handle mouse input in lower layers later. realize whenever new press happens, , timer
might trigger e.g. during half-an-hour finger-move input.
the second way have mousearea
s report uppon handled signals, signal happend, reset timer
. unhandled signals, layer mousearea
beneath else, handle signals there catch has been falling through.
resorting c++ might create item
@ root of item
-tree, , override childmouseeventfitler
see answer here more on this.
in case should add mousearea
right inside item
, has filter @ place.
note! method triggered each
mousearea
might under click. in scenario, fine, guess.
thanks grecko looked general eventfilter
again, , indeed easy.
- you create simple
qobject
following singleton pattern, in reimplementeventfilter
-method, emit signal
mouseeventspy.h
#pragma once #include <qobject> #include <qtqml> #include <qqmlengine> #include <qjsengine> class mouseeventspy : public qobject { q_object public: explicit mouseeventspy(qobject *parent = 0); static mouseeventspy* instance(); static qobject* singletonprovider(qqmlengine* engine, qjsengine* script); protected: bool eventfilter(qobject* watched, qevent* event); signals: void mouseeventdetected(/*pass meaningfull information qml?*/); };
mouseeventspy.cpp
#include "mouseeventspy.h" #include <qqmlengine> #include <qjsengine> #include <qevent> mouseeventspy::mouseeventspy(qobject *parent) : qobject(parent) { qdebug() << "created instance"; } // implements singleton pattern (*usually evil*) // can instance in c++ mouseeventspy* mouseeventspy::instance() { static mouseeventspy* inst; if (inst == nullptr) { // if no instance has been created yet, creat new , install event filter. // uppon first use of instance, automatically // install in qguiapplication inst = new mouseeventspy(); qguiapplication* app = qguiapp; app->installeventfilter(inst); } return inst; } // method fullfill signature required // qmlregistersingletontype. qobject* mouseeventspy::singletonprovider(qqmlengine *, qjsengine *) { return mouseeventspy::instance(); } // method necessary 'installeventfilter' bool mouseeventspy::eventfilter(qobject* watched, qevent* event) { qevent::type t = event->type(); if ((t == qevent::mousebuttondblclick || t == qevent::mousebuttonpress || t == qevent::mousebuttonrelease || t == qevent::mousemove) && event->spontaneous() // take mouse events outside of qt ) emit mouseeventdetected(); return qobject::eventfilter(watched, event); }
- than register singleton type qml this:
main.cpp
#include <qguiapplication> #include <qqmlapplicationengine> #include "mouseeventspy.h" int main(int argc, char *argv[]) { qguiapplication app(argc, argv); qqmlapplicationengine engine; qmlregistersingletontype<mouseeventspy>("mouseeventspy", 1, 0, "mouseeventspy", mouseeventspy::singletonprovider); // uppon creation of first instance. // app.installeventfilter(mouseeventspy::instance()); engine.load(qurl(qstringliteral("main.qml"))); return app.exec(); }
- now in qml can import instance of singleton in necessary files , use signal, e.g. reset
timer
main.qml
import qtquick 2.6 import qtquick.window 2.2 import mouseeventspy 1.0 window { visible: true width: 640 height: 480 title: qstr("hello world") connections { target: mouseeventspy onmouseeventdetected: mytimer.restart() } timer { id: mytimer interval: 1000 ontriggered: console.log('it has been 1 seconds since last mouse event') } text { anchors.center: parent text: mytimer.running ? 'timer running\nmove mouse reset' : 'move mouse make timer run again.' } }
Comments
Post a Comment