mirror of
https://github.com/crystalidea/qt6windows7.git
synced 2025-07-05 08:45:25 +08:00
qt 6.5.1 original
This commit is contained in:
42
examples/dbus/remotecontrolledcar/car/CMakeLists.txt
Normal file
42
examples/dbus/remotecontrolledcar/car/CMakeLists.txt
Normal file
@ -0,0 +1,42 @@
|
||||
# Copyright (C) 2022 The Qt Company Ltd.
|
||||
# SPDX-License-Identifier: BSD-3-Clause
|
||||
|
||||
set(CMAKE_INCLUDE_CURRENT_DIR ON)
|
||||
|
||||
if(NOT DEFINED INSTALL_EXAMPLESDIR)
|
||||
set(INSTALL_EXAMPLESDIR "examples")
|
||||
endif()
|
||||
|
||||
set(INSTALL_EXAMPLEDIR "${INSTALL_EXAMPLESDIR}/dbus/remotecontrolledcar/car")
|
||||
|
||||
set(car_SRCS)
|
||||
qt_add_dbus_adaptor(car_SRCS
|
||||
../common/car.xml
|
||||
qobject.h
|
||||
"" # empty parent_class value on purpose to not pass -l flag
|
||||
car_adaptor
|
||||
)
|
||||
|
||||
qt_add_executable(car
|
||||
car.cpp car.h
|
||||
main.cpp
|
||||
${car_SRCS}
|
||||
)
|
||||
|
||||
set_target_properties(car PROPERTIES
|
||||
WIN32_EXECUTABLE TRUE
|
||||
MACOSX_BUNDLE TRUE
|
||||
)
|
||||
|
||||
target_link_libraries(car PRIVATE
|
||||
Qt6::Core
|
||||
Qt6::DBus
|
||||
Qt6::Gui
|
||||
Qt6::Widgets
|
||||
)
|
||||
|
||||
install(TARGETS car
|
||||
RUNTIME DESTINATION "${INSTALL_EXAMPLEDIR}"
|
||||
BUNDLE DESTINATION "${INSTALL_EXAMPLEDIR}"
|
||||
LIBRARY DESTINATION "${INSTALL_EXAMPLEDIR}"
|
||||
)
|
97
examples/dbus/remotecontrolledcar/car/car.cpp
Normal file
97
examples/dbus/remotecontrolledcar/car/car.cpp
Normal file
@ -0,0 +1,97 @@
|
||||
// Copyright (C) 2016 The Qt Company Ltd.
|
||||
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause
|
||||
|
||||
#include "car.h"
|
||||
#include <QtWidgets/QtWidgets>
|
||||
#include <cmath>
|
||||
|
||||
QRectF Car::boundingRect() const
|
||||
{
|
||||
return QRectF(-35, -81, 70, 115);
|
||||
}
|
||||
|
||||
Car::Car()
|
||||
{
|
||||
startTimer(1000 / 33);
|
||||
setFlags(ItemIsMovable | ItemIsFocusable);
|
||||
}
|
||||
|
||||
void Car::accelerate()
|
||||
{
|
||||
if (speed < 10)
|
||||
++speed;
|
||||
}
|
||||
|
||||
void Car::decelerate()
|
||||
{
|
||||
if (speed > -10)
|
||||
--speed;
|
||||
}
|
||||
|
||||
void Car::turnLeft()
|
||||
{
|
||||
if (wheelsAngle > -30)
|
||||
wheelsAngle -= 5;
|
||||
}
|
||||
|
||||
void Car::turnRight()
|
||||
{
|
||||
if (wheelsAngle < 30)
|
||||
wheelsAngle += 5;
|
||||
}
|
||||
|
||||
void Car::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
|
||||
{
|
||||
Q_UNUSED(option);
|
||||
Q_UNUSED(widget);
|
||||
|
||||
painter->setBrush(Qt::gray);
|
||||
painter->drawRect(-20, -58, 40, 2); // front axel
|
||||
painter->drawRect(-20, 7, 40, 2); // rear axel
|
||||
|
||||
painter->setBrush(color);
|
||||
painter->drawRect(-25, -79, 50, 10); // front wing
|
||||
|
||||
painter->drawEllipse(-25, -48, 50, 20); // side pods
|
||||
painter->drawRect(-25, -38, 50, 35); // side pods
|
||||
painter->drawRect(-5, 9, 10, 10); // back pod
|
||||
|
||||
painter->drawEllipse(-10, -81, 20, 100); // main body
|
||||
|
||||
painter->drawRect(-17, 19, 34, 15); // rear wing
|
||||
|
||||
painter->setBrush(Qt::black);
|
||||
painter->drawPie(-5, -51, 10, 15, 0, 180 * 16);
|
||||
painter->drawRect(-5, -44, 10, 10); // cocpit
|
||||
|
||||
painter->save();
|
||||
painter->translate(-20, -58);
|
||||
painter->rotate(wheelsAngle);
|
||||
painter->drawRect(-10, -7, 10, 15); // front left
|
||||
painter->restore();
|
||||
|
||||
painter->save();
|
||||
painter->translate(20, -58);
|
||||
painter->rotate(wheelsAngle);
|
||||
painter->drawRect(0, -7, 10, 15); // front left
|
||||
painter->restore();
|
||||
|
||||
painter->drawRect(-30, 0, 12, 17); // rear left
|
||||
painter->drawRect(19, 0, 12, 17); // rear right
|
||||
}
|
||||
|
||||
void Car::timerEvent(QTimerEvent *event)
|
||||
{
|
||||
Q_UNUSED(event);
|
||||
|
||||
const qreal axelDistance = 54;
|
||||
qreal wheelsAngleRads = qDegreesToRadians(wheelsAngle);
|
||||
qreal turnDistance = std::cos(wheelsAngleRads) * axelDistance * 2;
|
||||
qreal turnRateRads = wheelsAngleRads / turnDistance; // rough estimate
|
||||
qreal turnRate = qRadiansToDegrees(turnRateRads);
|
||||
qreal rotation = speed * turnRate;
|
||||
|
||||
setTransform(QTransform().rotate(rotation), true);
|
||||
setTransform(QTransform::fromTranslate(0, -speed), true);
|
||||
update();
|
||||
}
|
34
examples/dbus/remotecontrolledcar/car/car.h
Normal file
34
examples/dbus/remotecontrolledcar/car/car.h
Normal file
@ -0,0 +1,34 @@
|
||||
// Copyright (C) 2016 The Qt Company Ltd.
|
||||
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause
|
||||
|
||||
#ifndef CAR_H
|
||||
#define CAR_H
|
||||
|
||||
#include <QGraphicsObject>
|
||||
#include <QBrush>
|
||||
|
||||
class Car : public QGraphicsObject
|
||||
{
|
||||
Q_OBJECT
|
||||
public:
|
||||
Car();
|
||||
QRectF boundingRect() const override;
|
||||
|
||||
public slots:
|
||||
void accelerate();
|
||||
void decelerate();
|
||||
void turnLeft();
|
||||
void turnRight();
|
||||
|
||||
protected:
|
||||
void paint(QPainter *painter, const QStyleOptionGraphicsItem *option,
|
||||
QWidget *widget = nullptr) override;
|
||||
void timerEvent(QTimerEvent *event) override;
|
||||
|
||||
private:
|
||||
QBrush color = Qt::green;
|
||||
qreal wheelsAngle = 0; // used when applying rotation
|
||||
qreal speed = 0; // delta movement along the body axis
|
||||
};
|
||||
|
||||
#endif // CAR_H
|
11
examples/dbus/remotecontrolledcar/car/car.pro
Normal file
11
examples/dbus/remotecontrolledcar/car/car.pro
Normal file
@ -0,0 +1,11 @@
|
||||
QT += dbus widgets
|
||||
|
||||
DBUS_ADAPTORS += ../common/car.xml
|
||||
HEADERS += car.h
|
||||
SOURCES += car.cpp main.cpp
|
||||
|
||||
CONFIG += no_batch # work around QTBUG-96513
|
||||
|
||||
# install
|
||||
target.path = $$[QT_INSTALL_EXAMPLES]/dbus/remotecontrolledcar/car
|
||||
INSTALLS += target
|
35
examples/dbus/remotecontrolledcar/car/main.cpp
Normal file
35
examples/dbus/remotecontrolledcar/car/main.cpp
Normal file
@ -0,0 +1,35 @@
|
||||
// Copyright (C) 2016 The Qt Company Ltd.
|
||||
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause
|
||||
|
||||
#include "car.h"
|
||||
#include "car_adaptor.h"
|
||||
#include <QtWidgets/QApplication>
|
||||
#include <QtWidgets/QGraphicsView>
|
||||
#include <QtWidgets/QGraphicsScene>
|
||||
#include <QtDBus/QDBusConnection>
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
QApplication app(argc, argv);
|
||||
|
||||
QGraphicsScene scene;
|
||||
scene.setSceneRect(-500, -500, 1000, 1000);
|
||||
scene.setItemIndexMethod(QGraphicsScene::NoIndex);
|
||||
|
||||
auto car = new Car();
|
||||
scene.addItem(car);
|
||||
|
||||
QGraphicsView view(&scene);
|
||||
view.setRenderHint(QPainter::Antialiasing);
|
||||
view.setBackgroundBrush(Qt::darkGray);
|
||||
view.setWindowTitle(QT_TRANSLATE_NOOP(QGraphicsView, "Qt DBus Controlled Car"));
|
||||
view.resize(400, 300);
|
||||
view.show();
|
||||
|
||||
new CarInterfaceAdaptor(car);
|
||||
auto connection = QDBusConnection::sessionBus();
|
||||
connection.registerObject("/Car", car);
|
||||
connection.registerService("org.example.CarExample");
|
||||
|
||||
return app.exec();
|
||||
}
|
Reference in New Issue
Block a user