Qt 6.x
The Qt SDK
Loading...
Searching...
No Matches
qtestsupport_widgets.cpp
Go to the documentation of this file.
1// Copyright (C) 2018 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
5
6#include "qwidget.h"
7
8#include <QtGui/qwindow.h>
9#include <QtCore/qtestsupport_core.h>
10#include <QtCore/qthread.h>
11#include <QtGui/qtestsupport_gui.h>
12#include <QtGui/private/qevent_p.h>
13#include <QtGui/private/qeventpoint_p.h>
14#include <private/qguiapplication_p.h>
15#include <qpa/qplatformintegration.h>
16
18
19template <typename FunctorWindowGetter, typename FunctorPredicate>
20static bool qWaitForWidgetWindow(FunctorWindowGetter windowGetter, FunctorPredicate predicate, int timeout)
21{
22 if (!windowGetter())
23 return false;
24
25 return QTest::qWaitFor([&]() {
26 if (QWindow *window = windowGetter())
27 return predicate(window);
28 return false;
29 }, timeout);
30}
31
49{
51 qWarning() << "qWaitForWindowActive was called on a platform that doesn't support window"
52 << "activation. This means there is an error in the test and it should either"
53 << "check for the WindowActivation platform capability before calling"
54 << "qWaitForWindowActivate, use qWaitForWindowExposed instead, or skip the test."
55 << "Falling back to qWaitForWindowExposed.";
57 }
58 return qWaitForWidgetWindow([&]() { return widget->window()->windowHandle(); },
59 [&](QWindow *window) { return window->isActive(); },
60 timeout);
61}
62
78{
79 return qWaitForWidgetWindow([&]() { return widget->window()->windowHandle(); },
80 [&](QWindow *window) { return window->isExposed(); },
81 timeout);
82}
83
84namespace QTest {
85
87{
90}
91
93{
94 auto &p = point(touchId);
95 QMutableEventPoint::setGlobalPosition(p, mapToScreen(widget, pt));
96 QMutableEventPoint::setState(p, QEventPoint::State::Pressed);
97 return *this;
98}
100{
101 auto &p = point(touchId);
102 QMutableEventPoint::setGlobalPosition(p, mapToScreen(widget, pt));
103 QMutableEventPoint::setState(p, QEventPoint::State::Updated);
104 return *this;
105}
107{
108 auto &p = point(touchId);
109 QMutableEventPoint::setGlobalPosition(p, mapToScreen(widget, pt));
110 QMutableEventPoint::setState(p, QEventPoint::State::Released);
111 return *this;
112}
113
115{
116 auto &p = pointOrPreviousPoint(touchId);
117 QMutableEventPoint::setState(p, QEventPoint::State::Stationary);
118 return *this;
119}
120
121bool QTouchEventWidgetSequence::commit(bool processEvents)
122{
123 bool ret = false;
124 if (points.isEmpty())
125 return ret;
126 QThread::sleep(std::chrono::milliseconds{1});
127 if (targetWindow) {
129 } else if (targetWidget) {
130 ret = qt_handleTouchEventv2(targetWidget->windowHandle(), device, points.values());
131 }
132 if (processEvents)
135 points.clear();
136 return ret;
137}
138
139QTest::QTouchEventWidgetSequence::QTouchEventWidgetSequence(QWidget *widget, QPointingDevice *aDevice, bool autoCommit)
140 : QTouchEventSequence(nullptr, aDevice, autoCommit), targetWidget(widget)
141{
142}
143
144QPoint QTouchEventWidgetSequence::mapToScreen(QWidget *widget, const QPoint &pt)
145{
146 if (widget)
147 return widget->mapToGlobal(pt);
148 return targetWidget ? targetWidget->mapToGlobal(pt) : pt;
149}
150
151} // namespace QTest
152
static void processEvents(QEventLoop::ProcessEventsFlags flags=QEventLoop::AllEvents)
Processes some pending events for the calling thread according to the specified flags.
static QPlatformIntegration * platformIntegration()
void clear()
Definition qmap.h:288
\inmodule QtCore\reentrant
Definition qpoint.h:23
The QPointingDevice class describes a device from which mouse, touch or tablet events originate.
QMap< int, QEventPoint > previousPoints
QEventPoint & pointOrPreviousPoint(int touchId)
QEventPoint & point(int touchId)
QMap< int, QEventPoint > points
bool commit(bool processEvents=true) override
QTouchEventWidgetSequence & press(int touchId, const QPoint &pt, QWidget *widget=nullptr)
QTouchEventWidgetSequence & stationary(int touchId) override
QTouchEventWidgetSequence & move(int touchId, const QPoint &pt, QWidget *widget=nullptr)
QTouchEventWidgetSequence & release(int touchId, const QPoint &pt, QWidget *widget=nullptr)
static void sleep(unsigned long)
The QWidget class is the base class of all user interface objects.
Definition qwidget.h:99
QWidget * window() const
Returns the window for this widget, i.e.
Definition qwidget.cpp:4320
QPointF mapToGlobal(const QPointF &) const
Translates the widget coordinate pos to global screen coordinates.
QWindow * windowHandle() const
If this is a native widget, return the associated QWindow.
Definition qwidget.cpp:2490
\inmodule QtGui
Definition qwindow.h:63
QOpenGLWidget * widget
[1]
const auto predicate
Combined button and popup list for selecting options.
Q_GUI_EXPORT bool qWaitForWindowActive(QWindow *window, int timeout=5000)
Q_GUI_EXPORT bool qWaitForWindowExposed(QWindow *window, int timeout=5000)
static bool qWaitFor(Functor predicate, QDeadlineTimer deadline=QDeadlineTimer(std::chrono::seconds{5}))
#define Q_UNLIKELY(x)
#define qWarning
Definition qlogging.h:162
return ret
GLbitfield GLuint64 timeout
[4]
GLfixed GLfixed GLint GLint GLfixed points
GLfloat GLfloat p
[1]
Q_GUI_EXPORT bool qt_handleTouchEventv2(QWindow *w, const QPointingDevice *device, const QList< QEventPoint > &points, Qt::KeyboardModifiers mods=Qt::NoModifier)
static QT_BEGIN_NAMESPACE bool qWaitForWidgetWindow(FunctorWindowGetter windowGetter, FunctorPredicate predicate, int timeout)
QObject::connect nullptr
aWidget window() -> setWindowTitle("New Window Title")
[2]