mirror of
https://github.com/crystalidea/qt6windows7.git
synced 2025-07-03 07:45:30 +08:00
qt 6.5.1 original
This commit is contained in:
12
examples/dbus/remotecontrolledcar/CMakeLists.txt
Normal file
12
examples/dbus/remotecontrolledcar/CMakeLists.txt
Normal file
@ -0,0 +1,12 @@
|
||||
# Copyright (C) 2022 The Qt Company Ltd.
|
||||
# SPDX-License-Identifier: BSD-3-Clause
|
||||
|
||||
cmake_minimum_required(VERSION 3.16)
|
||||
project(remotecontrolledcar LANGUAGES CXX)
|
||||
|
||||
find_package(Qt6 REQUIRED COMPONENTS Core DBus Gui Widgets)
|
||||
|
||||
qt_standard_project_setup()
|
||||
|
||||
add_subdirectory(car)
|
||||
add_subdirectory(controller)
|
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();
|
||||
}
|
10
examples/dbus/remotecontrolledcar/common/car.xml
Normal file
10
examples/dbus/remotecontrolledcar/common/car.xml
Normal file
@ -0,0 +1,10 @@
|
||||
<!DOCTYPE node PUBLIC "-//freedesktop//DTD D-BUS Object Introspection 1.0//EN"
|
||||
"http://www.freedesktop.org/standards/dbus/1.0/introspect.dtd">
|
||||
<node name="/com/trollech/examples/car">
|
||||
<interface name="org.example.Examples.CarInterface">
|
||||
<method name="accelerate"/>
|
||||
<method name="decelerate"/>
|
||||
<method name="turnLeft"/>
|
||||
<method name="turnRight"/>
|
||||
</interface>
|
||||
</node>
|
40
examples/dbus/remotecontrolledcar/controller/CMakeLists.txt
Normal file
40
examples/dbus/remotecontrolledcar/controller/CMakeLists.txt
Normal file
@ -0,0 +1,40 @@
|
||||
# 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/controller")
|
||||
|
||||
set(controller_SRCS)
|
||||
qt_add_dbus_interface(controller_SRCS
|
||||
../common/car.xml
|
||||
car_interface
|
||||
)
|
||||
|
||||
qt_add_executable(controller
|
||||
controller.cpp controller.h controller.ui
|
||||
main.cpp
|
||||
${controller_SRCS}
|
||||
)
|
||||
|
||||
set_target_properties(controller PROPERTIES
|
||||
WIN32_EXECUTABLE TRUE
|
||||
MACOSX_BUNDLE TRUE
|
||||
)
|
||||
|
||||
target_link_libraries(controller PRIVATE
|
||||
Qt6::Core
|
||||
Qt6::DBus
|
||||
Qt6::Gui
|
||||
Qt6::Widgets
|
||||
)
|
||||
|
||||
install(TARGETS controller
|
||||
RUNTIME DESTINATION "${INSTALL_EXAMPLEDIR}"
|
||||
BUNDLE DESTINATION "${INSTALL_EXAMPLEDIR}"
|
||||
LIBRARY DESTINATION "${INSTALL_EXAMPLEDIR}"
|
||||
)
|
25
examples/dbus/remotecontrolledcar/controller/controller.cpp
Normal file
25
examples/dbus/remotecontrolledcar/controller/controller.cpp
Normal file
@ -0,0 +1,25 @@
|
||||
// Copyright (C) 2016 The Qt Company Ltd.
|
||||
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause
|
||||
|
||||
#include "controller.h"
|
||||
|
||||
using org::example::Examples::CarInterface;
|
||||
|
||||
Controller::Controller(QWidget *parent) : QWidget(parent)
|
||||
{
|
||||
ui.setupUi(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);
|
||||
ui.label->setText(car->isValid() ? tr("connected") : tr("disconnected"));
|
||||
}
|
26
examples/dbus/remotecontrolledcar/controller/controller.h
Normal file
26
examples/dbus/remotecontrolledcar/controller/controller.h
Normal file
@ -0,0 +1,26 @@
|
||||
// Copyright (C) 2016 The Qt Company Ltd.
|
||||
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause
|
||||
|
||||
#ifndef CONTROLLER_H
|
||||
#define CONTROLLER_H
|
||||
|
||||
#include "ui_controller.h"
|
||||
#include "car_interface.h"
|
||||
|
||||
class Controller : public QWidget
|
||||
{
|
||||
Q_OBJECT
|
||||
|
||||
public:
|
||||
explicit Controller(QWidget *parent = nullptr);
|
||||
|
||||
protected:
|
||||
void timerEvent(QTimerEvent *event) override;
|
||||
|
||||
private:
|
||||
Ui::Controller ui;
|
||||
org::example::Examples::CarInterface *car;
|
||||
};
|
||||
|
||||
#endif
|
||||
|
13
examples/dbus/remotecontrolledcar/controller/controller.pro
Normal file
13
examples/dbus/remotecontrolledcar/controller/controller.pro
Normal file
@ -0,0 +1,13 @@
|
||||
QT += dbus widgets
|
||||
|
||||
DBUS_INTERFACES += ../common/car.xml
|
||||
FORMS += controller.ui
|
||||
HEADERS += controller.h
|
||||
SOURCES += main.cpp controller.cpp
|
||||
|
||||
# Work-around CI issue. Not needed in user code.
|
||||
CONFIG += no_batch
|
||||
|
||||
# install
|
||||
target.path = $$[QT_INSTALL_EXAMPLES]/dbus/remotecontrolledcar/controller
|
||||
INSTALLS += target
|
64
examples/dbus/remotecontrolledcar/controller/controller.ui
Normal file
64
examples/dbus/remotecontrolledcar/controller/controller.ui
Normal file
@ -0,0 +1,64 @@
|
||||
<ui version="4.0" >
|
||||
<class>Controller</class>
|
||||
<widget class="QWidget" name="Controller" >
|
||||
<property name="geometry" >
|
||||
<rect>
|
||||
<x>0</x>
|
||||
<y>0</y>
|
||||
<width>255</width>
|
||||
<height>111</height>
|
||||
</rect>
|
||||
</property>
|
||||
<property name="windowTitle" >
|
||||
<string>Controller</string>
|
||||
</property>
|
||||
<layout class="QGridLayout" >
|
||||
<property name="margin" >
|
||||
<number>9</number>
|
||||
</property>
|
||||
<property name="spacing" >
|
||||
<number>6</number>
|
||||
</property>
|
||||
<item row="1" column="1" >
|
||||
<widget class="QLabel" name="label" >
|
||||
<property name="text" >
|
||||
<string>Controller</string>
|
||||
</property>
|
||||
<property name="alignment" >
|
||||
<set>Qt::AlignCenter</set>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
<item row="2" column="1" >
|
||||
<widget class="QPushButton" name="decelerate" >
|
||||
<property name="text" >
|
||||
<string>Decelerate</string>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
<item row="0" column="1" >
|
||||
<widget class="QPushButton" name="accelerate" >
|
||||
<property name="text" >
|
||||
<string>Accelerate</string>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
<item row="1" column="2" >
|
||||
<widget class="QPushButton" name="right" >
|
||||
<property name="text" >
|
||||
<string>Right</string>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
<item row="1" column="0" >
|
||||
<widget class="QPushButton" name="left" >
|
||||
<property name="text" >
|
||||
<string>Left</string>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
</layout>
|
||||
</widget>
|
||||
<resources/>
|
||||
<connections/>
|
||||
</ui>
|
15
examples/dbus/remotecontrolledcar/controller/main.cpp
Normal file
15
examples/dbus/remotecontrolledcar/controller/main.cpp
Normal file
@ -0,0 +1,15 @@
|
||||
// Copyright (C) 2016 The Qt Company Ltd.
|
||||
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause
|
||||
|
||||
#include <QtWidgets>
|
||||
|
||||
|
||||
#include "controller.h"
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
QApplication app(argc, argv);
|
||||
Controller controller;
|
||||
controller.show();
|
||||
return app.exec();
|
||||
}
|
Binary file not shown.
After Width: | Height: | Size: 14 KiB |
@ -0,0 +1,16 @@
|
||||
// Copyright (C) 2016 The Qt Company Ltd.
|
||||
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GFDL-1.3-no-invariants-only
|
||||
|
||||
/*!
|
||||
\example remotecontrolledcar
|
||||
\title D-Bus Remote Controlled Car
|
||||
\ingroup examples-dbus
|
||||
\brief Shows how to use Qt D-Bus to control a car from another application.
|
||||
|
||||
The Remote Controlled Car example shows how to use \l{Qt D-Bus} to control
|
||||
one application from another.
|
||||
|
||||
\image remotecontrolledcar-car-example.webp
|
||||
|
||||
\include examples-run.qdocinc
|
||||
*/
|
@ -0,0 +1,3 @@
|
||||
TEMPLATE = subdirs
|
||||
SUBDIRS = car \
|
||||
controller
|
Reference in New Issue
Block a user