gpt4 book ai didi

qt - 每次从 qml 页面收到触摸事件时如何重置计时器

转载 作者:行者123 更新时间:2023-12-01 00:24:19 28 4
gpt4 key购买 nike

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 飞思卡尔设备开发了一个软件。

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

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

最佳答案

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

如果您不需要保证计时器在输入期间触发,您可以将 MouseArea 分层。在一切之上。在此 MouseArea您处理 pressed - 信号,但不要accept他们。

这允许您稍后处理较低层中的鼠标输入。然而,您只有在新媒体出现时才会意识到,Timer可能会触发例如在半小时的手指移动输入期间。

第二种方式是拥有所有MouseArea s 报告他们处理的信号,信号发生,重置 Timer .对于所有未处理的信号,您将 MouseArea 分层在其他一切之下,处理那里的所有信号以捕捉已经发生的事情。

求助于 C++ 你可能会创建一个 Item在您的 Item 的根-tree,并覆盖 childMouseEventFitler看我的回答 here有关更多信息。

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

Note! This method will be triggered for each MouseArea that might be under your click. But in your scenario, this would be fine, I guess.



感谢 GrecKo,我调查了一般情况 eventFilter再次,确实很容易。
  • 您创建一个简单的QObject遵循单例模式,在其中重新实现 eventFilter -method,这样它就会发出信号

  • 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 to QML?*/);

    };

    mouseeventspy.cpp
    #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);
    }
  • 比你像这样将它注册为单例类型到 QML:

  • 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);

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

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

    return app.exec();
    }
  • 现在在 QML 中,您可以在必要的文件中导入单例的实例并使用信号,例如重置 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 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.'
    }

    }

    关于qt - 每次从 qml 页面收到触摸事件时如何重置计时器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46173105/

    28 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com