From f7167ae82fd74d69f2b138fb60a36ed7f863b380 Mon Sep 17 00:00:00 2001 From: Ievgenii Meshcheriakov Date: Mon, 13 Mar 2023 14:21:04 +0100 Subject: [PATCH] remotecontrolledcar example: Modernize the code MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Also remove unused crashed() signal. Task-number: QTBUG-111366 Pick-to: 6.5 Change-Id: Ia23552a6396c324c7591643afaa634879a2c185e Reviewed-by: MÃ¥rten Nordheim Reviewed-by: Qt CI Bot --- examples/dbus/remotecontrolledcar/car/car.cpp | 9 ++-- examples/dbus/remotecontrolledcar/car/car.h | 16 ++++--- .../dbus/remotecontrolledcar/car/main.cpp | 4 +- .../dbus/remotecontrolledcar/common/car.xml | 3 +- .../controller/controller.cpp | 42 +++++-------------- .../controller/controller.h | 10 +---- 6 files changed, 27 insertions(+), 57 deletions(-) diff --git a/examples/dbus/remotecontrolledcar/car/car.cpp b/examples/dbus/remotecontrolledcar/car/car.cpp index 0c1ea3e2d7..7d12e54071 100644 --- a/examples/dbus/remotecontrolledcar/car/car.cpp +++ b/examples/dbus/remotecontrolledcar/car/car.cpp @@ -3,18 +3,17 @@ #include "car.h" #include -#include +#include QRectF Car::boundingRect() const { return QRectF(-35, -81, 70, 115); } -Car::Car() : color(Qt::green), wheelsAngle(0), speed(0) +Car::Car() { startTimer(1000 / 33); - setFlag(QGraphicsItem::ItemIsMovable, true); - setFlag(QGraphicsItem::ItemIsFocusable, true); + setFlags(ItemIsMovable | ItemIsFocusable); } void Car::accelerate() @@ -87,7 +86,7 @@ void Car::timerEvent(QTimerEvent *event) const qreal axelDistance = 54; qreal wheelsAngleRads = qDegreesToRadians(wheelsAngle); - qreal turnDistance = ::cos(wheelsAngleRads) * axelDistance * 2; + qreal turnDistance = std::cos(wheelsAngleRads) * axelDistance * 2; qreal turnRateRads = wheelsAngleRads / turnDistance; // rough estimate qreal turnRate = qRadiansToDegrees(turnRateRads); qreal rotation = speed * turnRate; diff --git a/examples/dbus/remotecontrolledcar/car/car.h b/examples/dbus/remotecontrolledcar/car/car.h index d21125a5cb..5a1e199ae0 100644 --- a/examples/dbus/remotecontrolledcar/car/car.h +++ b/examples/dbus/remotecontrolledcar/car/car.h @@ -12,7 +12,7 @@ class Car : public QGraphicsObject Q_OBJECT public: Car(); - QRectF boundingRect() const; + QRectF boundingRect() const override; public slots: void accelerate(); @@ -20,17 +20,15 @@ public slots: void turnLeft(); void turnRight(); -signals: - void crashed(); - protected: - void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget = nullptr); - void timerEvent(QTimerEvent *event); + void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, + QWidget *widget = nullptr) override; + void timerEvent(QTimerEvent *event) override; private: - QBrush color; - qreal wheelsAngle; // used when applying rotation - qreal speed; // delta movement along the body axis + QBrush color = Qt::green; + qreal wheelsAngle = 0; // used when applying rotation + qreal speed = 0; // delta movement along the body axis }; #endif // CAR_H diff --git a/examples/dbus/remotecontrolledcar/car/main.cpp b/examples/dbus/remotecontrolledcar/car/main.cpp index c7be72b275..9c732fed5e 100644 --- a/examples/dbus/remotecontrolledcar/car/main.cpp +++ b/examples/dbus/remotecontrolledcar/car/main.cpp @@ -16,7 +16,7 @@ int main(int argc, char *argv[]) scene.setSceneRect(-500, -500, 1000, 1000); scene.setItemIndexMethod(QGraphicsScene::NoIndex); - Car *car = new Car(); + auto car = new Car(); scene.addItem(car); QGraphicsView view(&scene); @@ -27,7 +27,7 @@ int main(int argc, char *argv[]) view.show(); new CarInterfaceAdaptor(car); - QDBusConnection connection = QDBusConnection::sessionBus(); + auto connection = QDBusConnection::sessionBus(); connection.registerObject("/Car", car); connection.registerService("org.example.CarExample"); diff --git a/examples/dbus/remotecontrolledcar/common/car.xml b/examples/dbus/remotecontrolledcar/common/car.xml index 6d8c9d19f2..ea9a000bb8 100644 --- a/examples/dbus/remotecontrolledcar/common/car.xml +++ b/examples/dbus/remotecontrolledcar/common/car.xml @@ -6,6 +6,5 @@ - - \ No newline at end of file + diff --git a/examples/dbus/remotecontrolledcar/controller/controller.cpp b/examples/dbus/remotecontrolledcar/controller/controller.cpp index ab0ec20d22..f5f0fa8866 100644 --- a/examples/dbus/remotecontrolledcar/controller/controller.cpp +++ b/examples/dbus/remotecontrolledcar/controller/controller.cpp @@ -1,45 +1,25 @@ // Copyright (C) 2016 The Qt Company Ltd. // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause -#include - #include "controller.h" -#include "car_interface.h" -Controller::Controller(QWidget *parent) - : QWidget(parent) +using org::example::Examples::CarInterface; + +Controller::Controller(QWidget *parent) : QWidget(parent) { ui.setupUi(this); - car = new org::example::Examples::CarInterface("org.example.CarExample", "/Car", - QDBusConnection::sessionBus(), this); + car = new CarInterface("org.example.CarExample", "/Car", QDBusConnection::sessionBus(), this); + + connect(ui.accelerate, &QPushButton::clicked, car, &CarInterface::accelerate); + connect(ui.decelerate, &QPushButton::clicked, car, &CarInterface::decelerate); + connect(ui.left, &QPushButton::clicked, car, &CarInterface::turnLeft); + connect(ui.right, &QPushButton::clicked, car, &CarInterface::turnRight); + startTimer(1000); } void Controller::timerEvent(QTimerEvent *event) { Q_UNUSED(event); - if (car->isValid()) - ui.label->setText("connected"); - else - ui.label->setText("disconnected"); -} - -void Controller::on_accelerate_clicked() -{ - car->accelerate(); -} - -void Controller::on_decelerate_clicked() -{ - car->decelerate(); -} - -void Controller::on_left_clicked() -{ - car->turnLeft(); -} - -void Controller::on_right_clicked() -{ - car->turnRight(); + ui.label->setText(car->isValid() ? tr("connected") : tr("disconnected")); } diff --git a/examples/dbus/remotecontrolledcar/controller/controller.h b/examples/dbus/remotecontrolledcar/controller/controller.h index c4d2838bfc..4fd1833330 100644 --- a/examples/dbus/remotecontrolledcar/controller/controller.h +++ b/examples/dbus/remotecontrolledcar/controller/controller.h @@ -12,16 +12,10 @@ class Controller : public QWidget Q_OBJECT public: - Controller(QWidget *parent = nullptr); + explicit Controller(QWidget *parent = nullptr); protected: - void timerEvent(QTimerEvent *event); - -private slots: - void on_accelerate_clicked(); - void on_decelerate_clicked(); - void on_left_clicked(); - void on_right_clicked(); + void timerEvent(QTimerEvent *event) override; private: Ui::Controller ui;