1// Copyright (C) 2016 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
4#ifndef QEVENTLOOP_H
5#define QEVENTLOOP_H
6
7#include <QtCore/qobject.h>
8
9QT_BEGIN_NAMESPACE
10
11class QEventLoopLocker;
12class QEventLoopPrivate;
13
14class Q_CORE_EXPORT QEventLoop : public QObject
15{
16 Q_OBJECT
17 Q_DECLARE_PRIVATE(QEventLoop)
18 friend class QEventLoopLocker;
19
20public:
21 explicit QEventLoop(QObject *parent = nullptr);
22 ~QEventLoop();
23
24 enum ProcessEventsFlag {
25 AllEvents = 0x00,
26 ExcludeUserInputEvents = 0x01,
27 ExcludeSocketNotifiers = 0x02,
28 WaitForMoreEvents = 0x04,
29 X11ExcludeTimers = 0x08,
30 EventLoopExec = 0x20,
31 DialogExec = 0x40,
32 ApplicationExec = 0x80,
33 };
34 Q_DECLARE_FLAGS(ProcessEventsFlags, ProcessEventsFlag)
35 Q_FLAG(ProcessEventsFlags)
36
37 bool processEvents(ProcessEventsFlags flags = AllEvents);
38 void processEvents(ProcessEventsFlags flags, int maximumTime);
39
40 int exec(ProcessEventsFlags flags = AllEvents);
41 bool isRunning() const;
42
43 void wakeUp();
44
45 bool event(QEvent *event) override;
46
47public Q_SLOTS:
48 void exit(int returnCode = 0);
49 void quit();
50};
51
52Q_DECLARE_OPERATORS_FOR_FLAGS(QEventLoop::ProcessEventsFlags)
53
54class QEventLoopLockerPrivate;
55
56class QEventLoopLocker
57{
58public:
59 Q_NODISCARD_CTOR Q_CORE_EXPORT QEventLoopLocker() noexcept;
60 Q_NODISCARD_CTOR Q_CORE_EXPORT explicit QEventLoopLocker(QEventLoop *loop) noexcept;
61 Q_NODISCARD_CTOR Q_CORE_EXPORT explicit QEventLoopLocker(QThread *thread) noexcept;
62 Q_CORE_EXPORT ~QEventLoopLocker();
63
64private:
65 Q_DISABLE_COPY(QEventLoopLocker)
66 friend class QEventLoopLockerPrivate;
67
68 //
69 // Private implementation details.
70 // Do not call from public inline API!
71 //
72 enum class Type : quintptr {
73 EventLoop,
74 Thread,
75 Application,
76 };
77 explicit QEventLoopLocker(void *ptr, Type t) noexcept;
78 quintptr p;
79 static constexpr quintptr TypeMask = 0x3;
80 Type type() const { return Type(p & TypeMask); }
81 void *pointer() const { return reinterpret_cast<void *>(p & ~TypeMask); }
82 template <typename Func>
83 void visit(Func func) const;
84};
85
86QT_END_NAMESPACE
87
88#endif // QEVENTLOOP_H
89

source code of qtbase/src/corelib/kernel/qeventloop.h