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 a software for embedded imx6 freescale device using qt framework.
Basically I just want to restart my timer every time I click and every time I get a touch event on my screen whether the click/touch happen inside the mouse area of my rectangles or outside of them.
The idea is similar to a screensaver.
There are multiple ways, and the right way depends on your requirements.
If you don't need to guarantee that the timer triggers during a input you can just layer a
MouseAreaon top of everything. In thisMouseAreayou handle thepressed-signals, but dontacceptthem.This allows you to handle the mouse input in the lower layers later. However you only realize whenever a new press happens, and the
Timermight trigger e.g. during a half-an-hour finger-move input.The second way is to have all
MouseAreas report uppon their handled signals, that the signal happend, to reset theTimer. For all unhandled signals, you layer aMouseAreabeneath everything else, handle all signals there to catch what has been falling through.Resorting to C++ you might create a
Itemat the root of yourItem-tree, and override thechildMouseEventFitlerSee my answer here for more on this.
In this case you should add a
MouseArearight inside thisItem, so it has something to filter at any place.Thanks to GrecKo I looked into the general
eventFilteragain, and indeed it is really easy.QObjectfollowing the singleton pattern, in which you reimplement theeventFilter-method, so that it will emit a signalmouseeventspy.h
mouseeventspy.cpp
main.cpp
Timermain.qml