remotecontrolledcar example: Modernize the code

Also remove unused crashed() signal.

Task-number: QTBUG-111366
Pick-to: 6.5
Change-Id: Ia23552a6396c324c7591643afaa634879a2c185e
Reviewed-by: Mårten Nordheim <marten.nordheim@qt.io>
Reviewed-by: Qt CI Bot <qt_ci_bot@qt-project.org>
This commit is contained in:
Ievgenii Meshcheriakov 2023-03-13 14:21:04 +01:00
parent fa3bfa72da
commit f7167ae82f
6 changed files with 27 additions and 57 deletions

View File

@ -3,18 +3,17 @@
#include "car.h" #include "car.h"
#include <QtWidgets/QtWidgets> #include <QtWidgets/QtWidgets>
#include <qmath.h> #include <cmath>
QRectF Car::boundingRect() const QRectF Car::boundingRect() const
{ {
return QRectF(-35, -81, 70, 115); return QRectF(-35, -81, 70, 115);
} }
Car::Car() : color(Qt::green), wheelsAngle(0), speed(0) Car::Car()
{ {
startTimer(1000 / 33); startTimer(1000 / 33);
setFlag(QGraphicsItem::ItemIsMovable, true); setFlags(ItemIsMovable | ItemIsFocusable);
setFlag(QGraphicsItem::ItemIsFocusable, true);
} }
void Car::accelerate() void Car::accelerate()
@ -87,7 +86,7 @@ void Car::timerEvent(QTimerEvent *event)
const qreal axelDistance = 54; const qreal axelDistance = 54;
qreal wheelsAngleRads = qDegreesToRadians(wheelsAngle); qreal wheelsAngleRads = qDegreesToRadians(wheelsAngle);
qreal turnDistance = ::cos(wheelsAngleRads) * axelDistance * 2; qreal turnDistance = std::cos(wheelsAngleRads) * axelDistance * 2;
qreal turnRateRads = wheelsAngleRads / turnDistance; // rough estimate qreal turnRateRads = wheelsAngleRads / turnDistance; // rough estimate
qreal turnRate = qRadiansToDegrees(turnRateRads); qreal turnRate = qRadiansToDegrees(turnRateRads);
qreal rotation = speed * turnRate; qreal rotation = speed * turnRate;

View File

@ -12,7 +12,7 @@ class Car : public QGraphicsObject
Q_OBJECT Q_OBJECT
public: public:
Car(); Car();
QRectF boundingRect() const; QRectF boundingRect() const override;
public slots: public slots:
void accelerate(); void accelerate();
@ -20,17 +20,15 @@ public slots:
void turnLeft(); void turnLeft();
void turnRight(); void turnRight();
signals:
void crashed();
protected: protected:
void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget = nullptr); void paint(QPainter *painter, const QStyleOptionGraphicsItem *option,
void timerEvent(QTimerEvent *event); QWidget *widget = nullptr) override;
void timerEvent(QTimerEvent *event) override;
private: private:
QBrush color; QBrush color = Qt::green;
qreal wheelsAngle; // used when applying rotation qreal wheelsAngle = 0; // used when applying rotation
qreal speed; // delta movement along the body axis qreal speed = 0; // delta movement along the body axis
}; };
#endif // CAR_H #endif // CAR_H

View File

@ -16,7 +16,7 @@ int main(int argc, char *argv[])
scene.setSceneRect(-500, -500, 1000, 1000); scene.setSceneRect(-500, -500, 1000, 1000);
scene.setItemIndexMethod(QGraphicsScene::NoIndex); scene.setItemIndexMethod(QGraphicsScene::NoIndex);
Car *car = new Car(); auto car = new Car();
scene.addItem(car); scene.addItem(car);
QGraphicsView view(&scene); QGraphicsView view(&scene);
@ -27,7 +27,7 @@ int main(int argc, char *argv[])
view.show(); view.show();
new CarInterfaceAdaptor(car); new CarInterfaceAdaptor(car);
QDBusConnection connection = QDBusConnection::sessionBus(); auto connection = QDBusConnection::sessionBus();
connection.registerObject("/Car", car); connection.registerObject("/Car", car);
connection.registerService("org.example.CarExample"); connection.registerService("org.example.CarExample");

View File

@ -6,6 +6,5 @@
<method name="decelerate"/> <method name="decelerate"/>
<method name="turnLeft"/> <method name="turnLeft"/>
<method name="turnRight"/> <method name="turnRight"/>
<signal name="crashed"/>
</interface> </interface>
</node> </node>

View File

@ -1,45 +1,25 @@
// Copyright (C) 2016 The Qt Company Ltd. // Copyright (C) 2016 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause
#include <QtWidgets>
#include "controller.h" #include "controller.h"
#include "car_interface.h"
Controller::Controller(QWidget *parent) using org::example::Examples::CarInterface;
: QWidget(parent)
Controller::Controller(QWidget *parent) : QWidget(parent)
{ {
ui.setupUi(this); ui.setupUi(this);
car = new org::example::Examples::CarInterface("org.example.CarExample", "/Car", car = new CarInterface("org.example.CarExample", "/Car", QDBusConnection::sessionBus(), this);
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); startTimer(1000);
} }
void Controller::timerEvent(QTimerEvent *event) void Controller::timerEvent(QTimerEvent *event)
{ {
Q_UNUSED(event); Q_UNUSED(event);
if (car->isValid()) ui.label->setText(car->isValid() ? tr("connected") : tr("disconnected"));
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();
} }

View File

@ -12,16 +12,10 @@ class Controller : public QWidget
Q_OBJECT Q_OBJECT
public: public:
Controller(QWidget *parent = nullptr); explicit Controller(QWidget *parent = nullptr);
protected: protected:
void timerEvent(QTimerEvent *event); void timerEvent(QTimerEvent *event) override;
private slots:
void on_accelerate_clicked();
void on_decelerate_clicked();
void on_left_clicked();
void on_right_clicked();
private: private:
Ui::Controller ui; Ui::Controller ui;