summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorIevgenii Meshcheriakov <ievgenii.meshcheriakov@qt.io>2023-03-31 15:45:19 +0200
committerQt Cherry-pick Bot <cherrypick_bot@qt-project.org>2023-04-20 13:10:47 +0000
commitd2b42d1dd0440776cf8e72ae79883b893b265d6f (patch)
tree1f9f30bde7c3b0c3c89f83facfcd351f1357afd2
parent3335e04cc635b60d9a3d53aeba6eb87b01bd336d (diff)
pingpong example modernization
Use constexpr to declare custom events. Reformat the code in some places using clang-format. Use qInfo() instead of std::cout. Use 'auto' where appropriate. Use per-class includes. Task-number: QTBUG-111448 Change-Id: I76222fd91663828bff7e5bab8187845627615521 Reviewed-by: Juha Vuolle <juha.vuolle@qt.io> (cherry picked from commit 907f7e3ac2be9dca4cbbce5495ff7e4f212de45a) Reviewed-by: Qt Cherry-pick Bot <cherrypick_bot@qt-project.org>
-rw-r--r--examples/statemachine/pingpong/main.cpp52
1 files changed, 26 insertions, 26 deletions
diff --git a/examples/statemachine/pingpong/main.cpp b/examples/statemachine/pingpong/main.cpp
index 27ca34e..a042e8b 100644
--- a/examples/statemachine/pingpong/main.cpp
+++ b/examples/statemachine/pingpong/main.cpp
@@ -1,24 +1,27 @@
// Copyright (C) 2016 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause
-#include <QtCore>
-#include <QtStateMachine>
+#include <QtCore/QCoreApplication>
+#include <QtCore/QDebug>
+#include <QtCore/QEvent>
+#include <QtStateMachine/QAbstractTransition>
+#include <QtStateMachine/QState>
+#include <QtStateMachine/QStateMachine>
-#include <iostream>
+static constexpr QEvent::Type PingEventType = QEvent::Type(QEvent::User + 2);
+static constexpr QEvent::Type PongEventType = QEvent::Type(QEvent::User + 3);
//! [0]
class PingEvent : public QEvent
{
public:
- PingEvent() : QEvent(QEvent::Type(QEvent::User+2))
- {}
+ PingEvent() : QEvent(PingEventType) { }
};
class PongEvent : public QEvent
{
public:
- PongEvent() : QEvent(QEvent::Type(QEvent::User+3))
- {}
+ PongEvent() : QEvent(PongEventType) { }
};
//! [0]
@@ -26,14 +29,13 @@ public:
class Pinger : public QState
{
public:
- Pinger(QState *parent)
- : QState(parent) {}
+ explicit Pinger(QState *parent) : QState(parent) { }
protected:
void onEntry(QEvent *) override
{
- machine()->postEvent(new PingEvent());
- std::cout << "ping?" << std::endl;
+ machine()->postEvent(new PingEvent);
+ qInfo() << "ping?";
}
};
//! [1]
@@ -45,13 +47,12 @@ public:
PongTransition() {}
protected:
- bool eventTest(QEvent *e) override {
- return (e->type() == QEvent::User+3);
- }
+ bool eventTest(QEvent *e) override { return (e->type() == PingEventType); }
+
void onTransition(QEvent *) override
{
- machine()->postDelayedEvent(new PingEvent(), 500);
- std::cout << "ping?" << std::endl;
+ machine()->postDelayedEvent(new PingEvent, 500);
+ qInfo() << "ping?";
}
};
//! [3]
@@ -63,13 +64,12 @@ public:
PingTransition() {}
protected:
- bool eventTest(QEvent *e) override {
- return (e->type() == QEvent::User+2);
- }
+ bool eventTest(QEvent *e) override { return e->type() == PingEventType; }
+
void onTransition(QEvent *) override
{
- machine()->postDelayedEvent(new PongEvent(), 500);
- std::cout << "pong!" << std::endl;
+ machine()->postDelayedEvent(new PongEvent, 500);
+ qInfo() << "pong!";
}
};
//! [2]
@@ -80,18 +80,18 @@ int main(int argc, char **argv)
QCoreApplication app(argc, argv);
QStateMachine machine;
- QState *group = new QState(QState::ParallelStates);
+ auto group = new QState(QState::ParallelStates);
group->setObjectName("group");
//! [4]
//! [5]
- Pinger *pinger = new Pinger(group);
+ auto pinger = new Pinger(group);
pinger->setObjectName("pinger");
- pinger->addTransition(new PongTransition());
+ pinger->addTransition(new PongTransition);
- QState *ponger = new QState(group);
+ auto ponger = new QState(group);
ponger->setObjectName("ponger");
- ponger->addTransition(new PingTransition());
+ ponger->addTransition(new PingTransition);
//! [5]
//! [6]