diff options
author | Ievgenii Meshcheriakov <ievgenii.meshcheriakov@qt.io> | 2023-03-31 15:45:19 +0200 |
---|---|---|
committer | Qt Cherry-pick Bot <cherrypick_bot@qt-project.org> | 2023-04-20 13:10:07 +0000 |
commit | 3335e04cc635b60d9a3d53aeba6eb87b01bd336d (patch) | |
tree | 6fedcae49fca9b499c83f9171eae35d3602c4546 | |
parent | 45d5dd2bb8943d8956524609a9133579674087a3 (diff) |
trafficlight example modernization
Add 'explicit' to some constructors.
Use inline class member initialization where possible.
Reformat some code using clang-format.
Add Q_OBJECT macros to QObjects.
Pass entire rectangle to QPainter::drawEllipse().
Use 'auto' where appropriate.
Add module prefix to Qt includes and sort them.
Task-number: QTBUG-111448
Change-Id: Ibfb49608b5f3710684b2b07d29b14ad03a8dfb24
Reviewed-by: Juha Vuolle <juha.vuolle@qt.io>
(cherry picked from commit 9c1a5a12fe8f49c7b1881f924e0b946bf8dc8232)
Reviewed-by: Qt Cherry-pick Bot <cherrypick_bot@qt-project.org>
-rw-r--r-- | examples/statemachine/trafficlight/main.cpp | 71 |
1 files changed, 35 insertions, 36 deletions
diff --git a/examples/statemachine/trafficlight/main.cpp b/examples/statemachine/trafficlight/main.cpp index a2478dc..5c85921 100644 --- a/examples/statemachine/trafficlight/main.cpp +++ b/examples/statemachine/trafficlight/main.cpp @@ -1,13 +1,13 @@ // Copyright (C) 2016 The Qt Company Ltd. // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause -#include <QApplication> -#include <QFinalState> -#include <QPainter> -#include <QStateMachine> -#include <QTimer> -#include <QVBoxLayout> -#include <QWidget> +#include <QtCore/QTimer> +#include <QtGui/QPainter> +#include <QtStateMachine/QFinalState> +#include <QtStateMachine/QStateMachine> +#include <QtWidgets/QApplication> +#include <QtWidgets/QVBoxLayout> +#include <QtWidgets/QWidget> //! [0] class LightWidget : public QWidget @@ -15,11 +15,13 @@ class LightWidget : public QWidget Q_OBJECT Q_PROPERTY(bool on READ isOn WRITE setOn) public: - LightWidget(const QColor &color, QWidget *parent = nullptr) - : QWidget(parent), m_color(color), m_on(false) {} + explicit LightWidget(const QColor &color, QWidget *parent = nullptr) + : QWidget(parent), m_color(color) + { + } + + bool isOn() const { return m_on; } - bool isOn() const - { return m_on; } void setOn(bool on) { if (on == m_on) @@ -40,41 +42,38 @@ protected: QPainter painter(this); painter.setRenderHint(QPainter::Antialiasing); painter.setBrush(m_color); - painter.drawEllipse(0, 0, width(), height()); + painter.drawEllipse(rect()); } private: QColor m_color; - bool m_on; + bool m_on = false; }; //! [0] //! [1] class TrafficLightWidget : public QWidget { + Q_OBJECT public: - TrafficLightWidget(QWidget *parent = nullptr) - : QWidget(parent) + explicit TrafficLightWidget(QWidget *parent = nullptr) : QWidget(parent) { - QVBoxLayout *vbox = new QVBoxLayout(this); + auto vbox = new QVBoxLayout(this); m_red = new LightWidget(Qt::red); vbox->addWidget(m_red); m_yellow = new LightWidget(Qt::yellow); vbox->addWidget(m_yellow); m_green = new LightWidget(Qt::green); vbox->addWidget(m_green); - QPalette pal = palette(); + auto pal = palette(); pal.setColor(QPalette::Window, Qt::black); setPalette(pal); setAutoFillBackground(true); } - LightWidget *redLight() const - { return m_red; } - LightWidget *yellowLight() const - { return m_yellow; } - LightWidget *greenLight() const - { return m_green; } + LightWidget *redLight() const { return m_red; } + LightWidget *yellowLight() const { return m_yellow; } + LightWidget *greenLight() const { return m_green; } private: LightWidget *m_red; @@ -86,15 +85,15 @@ private: //! [2] QState *createLightState(LightWidget *light, int duration, QState *parent = nullptr) { - QState *lightState = new QState(parent); - QTimer *timer = new QTimer(lightState); + auto lightState = new QState(parent); + auto timer = new QTimer(lightState); timer->setInterval(duration); timer->setSingleShot(true); - QState *timing = new QState(lightState); + auto timing = new QState(lightState); QObject::connect(timing, &QAbstractState::entered, light, &LightWidget::turnOn); QObject::connect(timing, &QAbstractState::entered, timer, QOverload<>::of(&QTimer::start)); QObject::connect(timing, &QAbstractState::exited, light, &LightWidget::turnOff); - QFinalState *done = new QFinalState(lightState); + auto done = new QFinalState(lightState); timing->addTransition(timer, &QTimer::timeout, done); lightState->setInitialState(timing); return lightState; @@ -104,25 +103,25 @@ QState *createLightState(LightWidget *light, int duration, QState *parent = null //! [3] class TrafficLight : public QWidget { + Q_OBJECT public: - TrafficLight(QWidget *parent = nullptr) - : QWidget(parent) + explicit TrafficLight(QWidget *parent = nullptr) : QWidget(parent) { - QVBoxLayout *vbox = new QVBoxLayout(this); - TrafficLightWidget *widget = new TrafficLightWidget; + auto vbox = new QVBoxLayout(this); + auto widget = new TrafficLightWidget; vbox->addWidget(widget); vbox->setContentsMargins(QMargins()); - QStateMachine *machine = new QStateMachine(this); - QState *redGoingYellow = createLightState(widget->redLight(), 3000); + auto machine = new QStateMachine(this); + auto redGoingYellow = createLightState(widget->redLight(), 3000); redGoingYellow->setObjectName("redGoingYellow"); - QState *yellowGoingGreen = createLightState(widget->yellowLight(), 1000); + auto yellowGoingGreen = createLightState(widget->yellowLight(), 1000); yellowGoingGreen->setObjectName("yellowGoingGreen"); redGoingYellow->addTransition(redGoingYellow, &QState::finished, yellowGoingGreen); - QState *greenGoingYellow = createLightState(widget->greenLight(), 3000); + auto greenGoingYellow = createLightState(widget->greenLight(), 3000); greenGoingYellow->setObjectName("greenGoingYellow"); yellowGoingGreen->addTransition(yellowGoingGreen, &QState::finished, greenGoingYellow); - QState *yellowGoingRed = createLightState(widget->yellowLight(), 1000); + auto yellowGoingRed = createLightState(widget->yellowLight(), 1000); yellowGoingRed->setObjectName("yellowGoingRed"); greenGoingYellow->addTransition(greenGoingYellow, &QState::finished, yellowGoingRed); yellowGoingRed->addTransition(yellowGoingRed, &QState::finished, redGoingYellow); |