Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
qquickvelocitycalculator.cpp
Go to the documentation of this file.
1// Copyright (C) 2025 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3// Qt-Security score:significant reason:default
4
6
7#include <QtCore/qdebug.h>
8
10
11/*
12 Usage:
13
14 QQuickVelocityCalculator velocityCalculator;
15
16 // ...
17
18 velocityCalcular.startMeasuring(event->pos(), event->timestamp());
19 velocityCalcular.stopMeasuring(event->pos(), event->timestamp());
20
21 // ...
22
23 if (velocityCalculator.velocity().x() > someAmount)
24 doSomething();
25 else if (velocityCalculator.velocity().x() < -someAmount)
26 doSomethingElse();
27*/
28
29void QQuickVelocityCalculator::startMeasuring(const QPointF &point1, qint64 timestamp)
30{
31 m_point1 = point1;
32 m_point1Timestamp = timestamp;
33}
34
35void QQuickVelocityCalculator::stopMeasuring(const QPointF &point2, qint64 timestamp)
36{
37 if (timestamp == 0) {
38 qWarning() << "QQuickVelocityCalculator: a call to stopMeasuring() must be preceded by a call to startMeasuring()";
39 return;
40 }
41
42 m_point2 = point2;
43 m_point2Timestamp = timestamp;
44}
45
46QPointF QQuickVelocityCalculator::velocity() const
47{
48 if (m_point2Timestamp == 0 || m_point1Timestamp == m_point2Timestamp)
49 return QPointF();
50
51 const qreal secondsElapsed = (m_point2Timestamp - m_point1Timestamp) / 1000.0;
52 const QPointF distance = m_point2 - m_point1;
53 return distance / secondsElapsed;
54}
55
56QT_END_NAMESPACE