首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何在每次从qml页面接收触摸事件时重置计时器?

如何在每次从qml页面接收触摸事件时重置计时器?
EN

Stack Overflow用户
提问于 2017-09-12 09:45:00
回答 1查看 3.2K关注 0票数 4
代码语言:javascript
运行
复制
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 ")

     }

   }
}  

我使用qt框架开发了嵌入式imx6飞思卡尔设备的软件。

基本上,我只想重新启动我的计时器,每次我点击,每次我得到一个触摸事件在我的屏幕上,无论是点击/触摸发生在我的矩形的鼠标区域内还是外部。

这个想法类似于屏幕保护程序。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-09-12 10:21:49

有多种方法,正确的方法取决于您的需求。

如果您不需要保证计时器在输入过程中触发,那么您可以在所有输入的基础上添加一个MouseArea。在这个MouseArea中,您处理pressed-signals,但不要对它们进行accept

这允许您稍后处理较低层中的鼠标输入。然而,只有当新的按下发生时,您才会意识到,Timer可能会触发,例如,在半小时的手指移动输入过程中。

第二种方法是让所有MouseArea的报告都显示它们处理的信号,即信号发生了,以重置Timer。对于所有未处理的信号,您可以将一个MouseArea分层到其他所有的东西下面,处理那里的所有信号,以捕捉掉下来的信号。

诉诸C++,您可以在Item-tree的根目录创建一个Item,并重写childMouseEventFitler

有关这方面的更多信息,请参见我的答案here

在这种情况下,您应该在这个MouseArea中添加一个Item,这样它就可以在任何地方过滤一些东西。

备注!将为您单击的每个MouseArea触发此方法。但在你的情况下,我想这会很好。

多亏了GrecKo,我再次查看了一般的eventFilter,这确实很容易。

  1. 按照单例模式创建一个简单的QObject,在该模式中重新实现eventFilter-method,以便它发出一个信号

mouseeventspy.h

代码语言:javascript
运行
复制
#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 to QML?*/);

};

mouseeventspy.cpp

代码语言:javascript
运行
复制
#include "mouseeventspy.h"
#include <QQmlEngine>
#include <QJSEngine>
#include <QEvent>

MouseEventSpy::MouseEventSpy(QObject *parent) : QObject(parent)
{
    qDebug() << "created Instance";
}

// This implements the SINGLETON PATTERN (*usually evil*)
// so you can get the instance in C++
MouseEventSpy* MouseEventSpy::instance()
{
    static MouseEventSpy* inst;
    if (inst == nullptr)
    {
        // If no instance has been created yet, creat a new and install it as event filter.
        // Uppon first use of the instance, it will automatically
        // install itself in the QGuiApplication
        inst = new MouseEventSpy();
        QGuiApplication* app = qGuiApp;
        app->installEventFilter(inst);
    }
    return inst;
}

// This is the method to fullfill the signature required by
// qmlRegisterSingletonType.
QObject* MouseEventSpy::singletonProvider(QQmlEngine *, QJSEngine *)
{
    return MouseEventSpy::instance();
}

// This is the method is necessary for '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 only mouse events from outside of Qt
            )
        emit mouseEventDetected();
    return QObject::eventFilter(watched, event);
}
  1. 而不是将其注册为单例类型QML,如下所示:

main.cpp

代码语言:javascript
运行
复制
#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);

    // We do this now uppon creation of the first instance.
    // app.installEventFilter(MouseEventSpy::instance());

    engine.load(QUrl(QStringLiteral("main.qml")));

    return app.exec();
}
  1. 现在,在QML中,您可以在必要的文件中导入单例实例并使用信号,例如重置Timer

main.qml

代码语言:javascript
运行
复制
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 the last mouse event')
    }

    Text {
        anchors.center: parent
        text: myTimer.running ? 'Timer is Running\nMove the mouse to reset'
                              : 'Move the Mouse to make the timer run again.'
    }

}
票数 5
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/46173105

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档