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 <QtWidgets/QtWidgets>
#include <qmath.h>
#include <cmath>
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;

View File

@ -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

View File

@ -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");

View File

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

View File

@ -1,45 +1,25 @@
// Copyright (C) 2016 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause
#include <QtWidgets>
#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"));
}

View File

@ -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;