mirror of
https://github.com/crystalidea/qt6windows7.git
synced 2025-07-05 16:55:25 +08:00
qt 6.5.1 original
This commit is contained in:
14
examples/dbus/CMakeLists.txt
Normal file
14
examples/dbus/CMakeLists.txt
Normal file
@ -0,0 +1,14 @@
|
||||
# Copyright (C) 2022 The Qt Company Ltd.
|
||||
# SPDX-License-Identifier: BSD-3-Clause
|
||||
|
||||
if(NOT TARGET Qt6::DBus)
|
||||
return()
|
||||
endif()
|
||||
qt_internal_add_example(pingpong)
|
||||
if(QT_FEATURE_process)
|
||||
qt_internal_add_example(complexpingpong)
|
||||
endif()
|
||||
if(TARGET Qt6::Widgets)
|
||||
qt_internal_add_example(chat)
|
||||
qt_internal_add_example(remotecontrolledcar)
|
||||
endif()
|
54
examples/dbus/chat/CMakeLists.txt
Normal file
54
examples/dbus/chat/CMakeLists.txt
Normal file
@ -0,0 +1,54 @@
|
||||
# Copyright (C) 2022 The Qt Company Ltd.
|
||||
# SPDX-License-Identifier: BSD-3-Clause
|
||||
|
||||
cmake_minimum_required(VERSION 3.16)
|
||||
project(chat LANGUAGES CXX)
|
||||
|
||||
set(CMAKE_INCLUDE_CURRENT_DIR ON)
|
||||
|
||||
if(NOT DEFINED INSTALL_EXAMPLESDIR)
|
||||
set(INSTALL_EXAMPLESDIR "examples")
|
||||
endif()
|
||||
|
||||
set(INSTALL_EXAMPLEDIR "${INSTALL_EXAMPLESDIR}/dbus/chat")
|
||||
|
||||
find_package(Qt6 REQUIRED COMPONENTS Core DBus Gui Widgets)
|
||||
|
||||
qt_standard_project_setup()
|
||||
|
||||
set(chat_SRCS)
|
||||
qt_add_dbus_interface(chat_SRCS
|
||||
org.example.chat.xml
|
||||
chat_interface
|
||||
)
|
||||
|
||||
qt_add_dbus_adaptor(chat_SRCS
|
||||
org.example.chat.xml
|
||||
qobject.h
|
||||
QObject
|
||||
chat_adaptor
|
||||
)
|
||||
|
||||
qt_add_executable(chat
|
||||
chat.cpp chat.h
|
||||
chatmainwindow.ui
|
||||
${chat_SRCS}
|
||||
)
|
||||
|
||||
set_target_properties(chat PROPERTIES
|
||||
WIN32_EXECUTABLE TRUE
|
||||
MACOSX_BUNDLE TRUE
|
||||
)
|
||||
|
||||
target_link_libraries(chat PRIVATE
|
||||
Qt6::Core
|
||||
Qt6::DBus
|
||||
Qt6::Gui
|
||||
Qt6::Widgets
|
||||
)
|
||||
|
||||
install(TARGETS chat
|
||||
RUNTIME DESTINATION "${INSTALL_EXAMPLEDIR}"
|
||||
BUNDLE DESTINATION "${INSTALL_EXAMPLEDIR}"
|
||||
LIBRARY DESTINATION "${INSTALL_EXAMPLEDIR}"
|
||||
)
|
91
examples/dbus/chat/chat.cpp
Normal file
91
examples/dbus/chat/chat.cpp
Normal file
@ -0,0 +1,91 @@
|
||||
// Copyright (C) 2016 The Qt Company Ltd.
|
||||
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause
|
||||
|
||||
#include <QApplication>
|
||||
#include <QInputDialog>
|
||||
#include <QMessageBox>
|
||||
|
||||
#include "chat.h"
|
||||
#include "chat_adaptor.h"
|
||||
#include "chat_interface.h"
|
||||
|
||||
ChatMainWindow::ChatMainWindow()
|
||||
{
|
||||
setupUi(this);
|
||||
|
||||
connect(messageLineEdit, &QLineEdit::textChanged, this,
|
||||
[this](const QString &newText) { sendButton->setEnabled(!newText.isEmpty()); });
|
||||
connect(sendButton, &QPushButton::clicked, this, [this]() {
|
||||
emit message(m_nickname, messageLineEdit->text());
|
||||
messageLineEdit->clear();
|
||||
});
|
||||
connect(actionChangeNickname, &QAction::triggered,
|
||||
this, &ChatMainWindow::changeNickname);
|
||||
connect(actionAboutQt, &QAction::triggered, this, [this]() { QMessageBox::aboutQt(this); });
|
||||
connect(qApp, &QApplication::lastWindowClosed, this,
|
||||
[this]() { emit action(m_nickname, tr("leaves the chat")); });
|
||||
|
||||
// add our D-Bus interface and connect to D-Bus
|
||||
new ChatAdaptor(this);
|
||||
|
||||
auto connection = QDBusConnection::sessionBus();
|
||||
connection.registerObject("/", this);
|
||||
|
||||
using org::example::chat;
|
||||
|
||||
auto *iface = new chat({}, {}, connection, this);
|
||||
connect(iface, &chat::message, this, [this](const QString &nickname, const QString &text) {
|
||||
displayMessage(tr("<%1> %2").arg(nickname, text));
|
||||
});
|
||||
connect(iface, &chat::action, this, [this](const QString &nickname, const QString &text) {
|
||||
displayMessage(tr("* %1 %2").arg(nickname, text));
|
||||
});
|
||||
|
||||
if (!changeNickname(true))
|
||||
QMetaObject::invokeMethod(qApp, &QApplication::quit, Qt::QueuedConnection);
|
||||
}
|
||||
|
||||
void ChatMainWindow::displayMessage(const QString &message)
|
||||
{
|
||||
m_messages.append(message);
|
||||
|
||||
if (m_messages.count() > 100)
|
||||
m_messages.removeFirst();
|
||||
|
||||
auto history = m_messages.join(QLatin1String("\n"));
|
||||
chatHistory->setPlainText(history);
|
||||
}
|
||||
|
||||
bool ChatMainWindow::changeNickname(bool initial)
|
||||
{
|
||||
auto newNickname = QInputDialog::getText(this, tr("Set nickname"), tr("New nickname:"));
|
||||
newNickname = newNickname.trimmed();
|
||||
|
||||
if (!newNickname.isEmpty()) {
|
||||
auto old = m_nickname;
|
||||
m_nickname = newNickname;
|
||||
|
||||
if (initial)
|
||||
emit action(m_nickname, tr("joins the chat"));
|
||||
else
|
||||
emit action(old, tr("is now known as %1").arg(m_nickname));
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
int main(int argc, char **argv)
|
||||
{
|
||||
QApplication app(argc, argv);
|
||||
|
||||
if (!QDBusConnection::sessionBus().isConnected()) {
|
||||
qWarning("Cannot connect to the D-Bus session bus.\n"
|
||||
"Please check your system settings and try again.\n");
|
||||
return 1;
|
||||
}
|
||||
|
||||
ChatMainWindow chat;
|
||||
chat.show();
|
||||
return app.exec();
|
||||
}
|
30
examples/dbus/chat/chat.h
Normal file
30
examples/dbus/chat/chat.h
Normal file
@ -0,0 +1,30 @@
|
||||
// Copyright (C) 2016 The Qt Company Ltd.
|
||||
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause
|
||||
|
||||
#ifndef CHAT_H
|
||||
#define CHAT_H
|
||||
|
||||
#include <QStringList>
|
||||
|
||||
#include "ui_chatmainwindow.h"
|
||||
|
||||
class ChatMainWindow: public QMainWindow, Ui::ChatMainWindow
|
||||
{
|
||||
Q_OBJECT
|
||||
QString m_nickname;
|
||||
QStringList m_messages;
|
||||
public:
|
||||
ChatMainWindow();
|
||||
|
||||
private:
|
||||
void displayMessage(const QString &message);
|
||||
|
||||
signals:
|
||||
void message(const QString &nickname, const QString &text);
|
||||
void action(const QString &nickname, const QString &text);
|
||||
|
||||
private slots:
|
||||
bool changeNickname(bool initial = false);
|
||||
};
|
||||
|
||||
#endif // CHAT_H
|
14
examples/dbus/chat/chat.pro
Normal file
14
examples/dbus/chat/chat.pro
Normal file
@ -0,0 +1,14 @@
|
||||
QT += dbus widgets
|
||||
|
||||
HEADERS += chat.h
|
||||
SOURCES += chat.cpp
|
||||
FORMS += chatmainwindow.ui
|
||||
|
||||
DBUS_ADAPTORS += org.example.chat.xml
|
||||
DBUS_INTERFACES += org.example.chat.xml
|
||||
|
||||
CONFIG += no_batch # work around QTBUG-96513
|
||||
|
||||
# install
|
||||
target.path = $$[QT_INSTALL_EXAMPLES]/dbus/chat
|
||||
INSTALLS += target
|
185
examples/dbus/chat/chatmainwindow.ui
Normal file
185
examples/dbus/chat/chatmainwindow.ui
Normal file
@ -0,0 +1,185 @@
|
||||
<ui version="4.0" >
|
||||
<author></author>
|
||||
<comment></comment>
|
||||
<exportmacro></exportmacro>
|
||||
<class>ChatMainWindow</class>
|
||||
<widget class="QMainWindow" name="ChatMainWindow" >
|
||||
<property name="geometry" >
|
||||
<rect>
|
||||
<x>0</x>
|
||||
<y>0</y>
|
||||
<width>800</width>
|
||||
<height>600</height>
|
||||
</rect>
|
||||
</property>
|
||||
<property name="windowTitle" >
|
||||
<string>Qt D-Bus Chat</string>
|
||||
</property>
|
||||
<widget class="QWidget" name="centralwidget" >
|
||||
<layout class="QHBoxLayout" >
|
||||
<property name="margin" >
|
||||
<number>9</number>
|
||||
</property>
|
||||
<property name="spacing" >
|
||||
<number>6</number>
|
||||
</property>
|
||||
<item>
|
||||
<layout class="QVBoxLayout" >
|
||||
<property name="margin" >
|
||||
<number>0</number>
|
||||
</property>
|
||||
<property name="spacing" >
|
||||
<number>6</number>
|
||||
</property>
|
||||
<item>
|
||||
<widget class="QTextBrowser" name="chatHistory" >
|
||||
<property name="acceptDrops" >
|
||||
<bool>false</bool>
|
||||
</property>
|
||||
<property name="toolTip" >
|
||||
<string>Messages sent and received from other users</string>
|
||||
</property>
|
||||
<property name="acceptRichText" >
|
||||
<bool>true</bool>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
<item>
|
||||
<layout class="QHBoxLayout" >
|
||||
<property name="margin" >
|
||||
<number>0</number>
|
||||
</property>
|
||||
<property name="spacing" >
|
||||
<number>6</number>
|
||||
</property>
|
||||
<item>
|
||||
<widget class="QLabel" name="label" >
|
||||
<property name="text" >
|
||||
<string>Message:</string>
|
||||
</property>
|
||||
<property name="buddy" >
|
||||
<cstring>messageLineEdit</cstring>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
<item>
|
||||
<widget class="QLineEdit" name="messageLineEdit" />
|
||||
</item>
|
||||
<item>
|
||||
<widget class="QPushButton" name="sendButton" >
|
||||
<property name="sizePolicy" >
|
||||
<sizepolicy>
|
||||
<hsizetype>1</hsizetype>
|
||||
<vsizetype>0</vsizetype>
|
||||
<horstretch>0</horstretch>
|
||||
<verstretch>0</verstretch>
|
||||
</sizepolicy>
|
||||
</property>
|
||||
<property name="toolTip" >
|
||||
<string>Sends a message to other people</string>
|
||||
</property>
|
||||
<property name="whatsThis" >
|
||||
<string/>
|
||||
</property>
|
||||
<property name="text" >
|
||||
<string>Send</string>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
</layout>
|
||||
</item>
|
||||
</layout>
|
||||
</item>
|
||||
</layout>
|
||||
</widget>
|
||||
<widget class="QMenuBar" name="menubar" >
|
||||
<property name="geometry" >
|
||||
<rect>
|
||||
<x>0</x>
|
||||
<y>0</y>
|
||||
<width>800</width>
|
||||
<height>31</height>
|
||||
</rect>
|
||||
</property>
|
||||
<widget class="QMenu" name="menuQuit" >
|
||||
<property name="title" >
|
||||
<string>Help</string>
|
||||
</property>
|
||||
<addaction name="actionAboutQt" />
|
||||
</widget>
|
||||
<widget class="QMenu" name="menuFile" >
|
||||
<property name="title" >
|
||||
<string>File</string>
|
||||
</property>
|
||||
<addaction name="actionChangeNickname" />
|
||||
<addaction name="separator" />
|
||||
<addaction name="actionQuit" />
|
||||
</widget>
|
||||
<addaction name="menuFile" />
|
||||
<addaction name="menuQuit" />
|
||||
</widget>
|
||||
<widget class="QStatusBar" name="statusbar" />
|
||||
<action name="actionQuit" >
|
||||
<property name="text" >
|
||||
<string>Quit</string>
|
||||
</property>
|
||||
<property name="shortcut" >
|
||||
<string>Ctrl+Q</string>
|
||||
</property>
|
||||
</action>
|
||||
<action name="actionAboutQt" >
|
||||
<property name="text" >
|
||||
<string>About Qt...</string>
|
||||
</property>
|
||||
</action>
|
||||
<action name="actionChangeNickname" >
|
||||
<property name="text" >
|
||||
<string>Change nickname...</string>
|
||||
</property>
|
||||
<property name="shortcut" >
|
||||
<string>Ctrl+N</string>
|
||||
</property>
|
||||
</action>
|
||||
</widget>
|
||||
<pixmapfunction></pixmapfunction>
|
||||
<tabstops>
|
||||
<tabstop>chatHistory</tabstop>
|
||||
<tabstop>messageLineEdit</tabstop>
|
||||
<tabstop>sendButton</tabstop>
|
||||
</tabstops>
|
||||
<resources/>
|
||||
<connections>
|
||||
<connection>
|
||||
<sender>messageLineEdit</sender>
|
||||
<signal>returnPressed()</signal>
|
||||
<receiver>sendButton</receiver>
|
||||
<slot>animateClick()</slot>
|
||||
<hints>
|
||||
<hint type="sourcelabel" >
|
||||
<x>299</x>
|
||||
<y>554</y>
|
||||
</hint>
|
||||
<hint type="destinationlabel" >
|
||||
<x>744</x>
|
||||
<y>551</y>
|
||||
</hint>
|
||||
</hints>
|
||||
</connection>
|
||||
<connection>
|
||||
<sender>actionQuit</sender>
|
||||
<signal>triggered(bool)</signal>
|
||||
<receiver>ChatMainWindow</receiver>
|
||||
<slot>close()</slot>
|
||||
<hints>
|
||||
<hint type="sourcelabel" >
|
||||
<x>-1</x>
|
||||
<y>-1</y>
|
||||
</hint>
|
||||
<hint type="destinationlabel" >
|
||||
<x>399</x>
|
||||
<y>299</y>
|
||||
</hint>
|
||||
</hints>
|
||||
</connection>
|
||||
</connections>
|
||||
</ui>
|
15
examples/dbus/chat/org.example.chat.xml
Normal file
15
examples/dbus/chat/org.example.chat.xml
Normal file
@ -0,0 +1,15 @@
|
||||
<!DOCTYPE node PUBLIC "-//freedesktop//DTD D-BUS Object Introspection 1.0//EN"
|
||||
"http://www.freedesktop.org/standards/dbus/1.0/introspect.dtd">
|
||||
<node>
|
||||
<interface name="org.example.chat">
|
||||
<signal name="message">
|
||||
<arg name="nickname" type="s" direction="out"/>
|
||||
<arg name="text" type="s" direction="out"/>
|
||||
</signal>
|
||||
<signal name="action">
|
||||
<arg name="nickname" type="s" direction="out"/>
|
||||
<arg name="text" type="s" direction="out"/>
|
||||
</signal>
|
||||
</interface>
|
||||
</node>
|
||||
|
40
examples/dbus/complexpingpong/CMakeLists.txt
Normal file
40
examples/dbus/complexpingpong/CMakeLists.txt
Normal file
@ -0,0 +1,40 @@
|
||||
# Copyright (C) 2022 The Qt Company Ltd.
|
||||
# SPDX-License-Identifier: BSD-3-Clause
|
||||
|
||||
cmake_minimum_required(VERSION 3.16)
|
||||
project(complexpingpong LANGUAGES CXX)
|
||||
|
||||
if(NOT DEFINED INSTALL_EXAMPLESDIR)
|
||||
set(INSTALL_EXAMPLESDIR "examples")
|
||||
endif()
|
||||
|
||||
set(INSTALL_EXAMPLEDIR "${INSTALL_EXAMPLESDIR}/dbus/complexpingpong")
|
||||
|
||||
find_package(Qt6 REQUIRED COMPONENTS Core DBus)
|
||||
|
||||
qt_standard_project_setup()
|
||||
|
||||
qt_add_executable(complexping
|
||||
complexping.cpp complexping.h
|
||||
ping-common.h
|
||||
)
|
||||
|
||||
target_link_libraries(complexping PRIVATE
|
||||
Qt6::Core
|
||||
Qt6::DBus
|
||||
)
|
||||
|
||||
qt_add_executable(complexpong
|
||||
complexpong.cpp complexpong.h
|
||||
)
|
||||
|
||||
target_link_libraries(complexpong PRIVATE
|
||||
Qt6::Core
|
||||
Qt6::DBus
|
||||
)
|
||||
|
||||
install(TARGETS complexping complexpong
|
||||
RUNTIME DESTINATION "${INSTALL_EXAMPLEDIR}"
|
||||
BUNDLE DESTINATION "${INSTALL_EXAMPLEDIR}"
|
||||
LIBRARY DESTINATION "${INSTALL_EXAMPLEDIR}"
|
||||
)
|
87
examples/dbus/complexpingpong/complexping.cpp
Normal file
87
examples/dbus/complexpingpong/complexping.cpp
Normal file
@ -0,0 +1,87 @@
|
||||
// Copyright (C) 2016 The Qt Company Ltd.
|
||||
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause
|
||||
|
||||
#include "ping-common.h"
|
||||
#include "complexping.h"
|
||||
|
||||
#include <QCoreApplication>
|
||||
#include <QDBusInterface>
|
||||
#include <QDBusReply>
|
||||
#include <QDBusServiceWatcher>
|
||||
#include <QDebug>
|
||||
#include <QProcess>
|
||||
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
|
||||
void Ping::start(const QString &name)
|
||||
{
|
||||
if (name != SERVICE_NAME)
|
||||
return;
|
||||
|
||||
auto connection = QDBusConnection::sessionBus();
|
||||
// find our remote
|
||||
auto iface = new QDBusInterface(SERVICE_NAME, "/", "org.example.QtDBus.ComplexPong.Pong",
|
||||
connection, this);
|
||||
if (!iface->isValid()) {
|
||||
qWarning().noquote() << connection.lastError().message();
|
||||
QCoreApplication::instance()->quit();
|
||||
}
|
||||
|
||||
connect(iface, SIGNAL(aboutToQuit()), QCoreApplication::instance(), SLOT(quit()));
|
||||
|
||||
std::string s;
|
||||
|
||||
while (true) {
|
||||
std::cout << qPrintable(tr("Ask your question: ")) << std::flush;
|
||||
|
||||
std::getline(std::cin, s);
|
||||
auto line = QString::fromStdString(s).trimmed();
|
||||
|
||||
if (line.isEmpty()) {
|
||||
iface->call("quit");
|
||||
return;
|
||||
} else if (line == "value") {
|
||||
QVariant reply = iface->property("value");
|
||||
if (!reply.isNull())
|
||||
std::cout << "value = " << qPrintable(reply.toString()) << std::endl;
|
||||
} else if (line.startsWith("value=")) {
|
||||
iface->setProperty("value", line.mid(6));
|
||||
} else {
|
||||
QDBusReply<QDBusVariant> reply = iface->call("query", line);
|
||||
if (reply.isValid()) {
|
||||
std::cout << qPrintable(tr("Reply was: %1").arg(reply.value().variant().toString()))
|
||||
<< std::endl;
|
||||
}
|
||||
}
|
||||
|
||||
if (iface->lastError().isValid())
|
||||
qWarning().noquote() << tr("Call failed: %1").arg(iface->lastError().message());
|
||||
}
|
||||
}
|
||||
|
||||
int main(int argc, char **argv)
|
||||
{
|
||||
QCoreApplication app(argc, argv);
|
||||
|
||||
if (!QDBusConnection::sessionBus().isConnected()) {
|
||||
qWarning().noquote() << QCoreApplication::translate(
|
||||
"complexping",
|
||||
"Cannot connect to the D-Bus session bus.\n"
|
||||
"To start it, run:\n"
|
||||
"\teval `dbus-launch --auto-syntax`");
|
||||
return 1;
|
||||
}
|
||||
|
||||
QDBusServiceWatcher serviceWatcher(SERVICE_NAME, QDBusConnection::sessionBus(),
|
||||
QDBusServiceWatcher::WatchForRegistration);
|
||||
|
||||
Ping ping;
|
||||
QObject::connect(&serviceWatcher, &QDBusServiceWatcher::serviceRegistered,
|
||||
&ping, &Ping::start);
|
||||
|
||||
QProcess pong;
|
||||
pong.start("./complexpong");
|
||||
|
||||
app.exec();
|
||||
}
|
16
examples/dbus/complexpingpong/complexping.h
Normal file
16
examples/dbus/complexpingpong/complexping.h
Normal file
@ -0,0 +1,16 @@
|
||||
// Copyright (C) 2016 The Qt Company Ltd.
|
||||
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause
|
||||
|
||||
#ifndef COMPLEXPING_H
|
||||
#define COMPLEXPING_H
|
||||
|
||||
#include <QtCore/QObject>
|
||||
|
||||
class Ping : public QObject
|
||||
{
|
||||
Q_OBJECT
|
||||
public slots:
|
||||
void start(const QString &name);
|
||||
};
|
||||
|
||||
#endif
|
8
examples/dbus/complexpingpong/complexping.pro
Normal file
8
examples/dbus/complexpingpong/complexping.pro
Normal file
@ -0,0 +1,8 @@
|
||||
QT -= gui
|
||||
QT += dbus
|
||||
|
||||
HEADERS += complexping.h ping-common.h
|
||||
SOURCES += complexping.cpp
|
||||
|
||||
target.path = $$[QT_INSTALL_EXAMPLES]/dbus/complexpingpong
|
||||
INSTALLS += target
|
3
examples/dbus/complexpingpong/complexpingpong.pro
Normal file
3
examples/dbus/complexpingpong/complexpingpong.pro
Normal file
@ -0,0 +1,3 @@
|
||||
TEMPLATE = subdirs
|
||||
win32:CONFIG += console
|
||||
SUBDIRS = complexping.pro complexpong.pro
|
66
examples/dbus/complexpingpong/complexpong.cpp
Normal file
66
examples/dbus/complexpingpong/complexpong.cpp
Normal file
@ -0,0 +1,66 @@
|
||||
// Copyright (C) 2016 The Qt Company Ltd.
|
||||
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause
|
||||
|
||||
#include "ping-common.h"
|
||||
#include "complexpong.h"
|
||||
|
||||
#include <QCoreApplication>
|
||||
#include <QDBusConnection>
|
||||
#include <QDBusError>
|
||||
#include <QDebug>
|
||||
|
||||
QString Pong::value() const
|
||||
{
|
||||
return m_value;
|
||||
}
|
||||
|
||||
void Pong::setValue(const QString &newValue)
|
||||
{
|
||||
m_value = newValue;
|
||||
}
|
||||
|
||||
void Pong::quit()
|
||||
{
|
||||
QMetaObject::invokeMethod(QCoreApplication::instance(), &QCoreApplication::quit,
|
||||
Qt::QueuedConnection);
|
||||
}
|
||||
|
||||
QDBusVariant Pong::query(const QString &query)
|
||||
{
|
||||
QString q = query.toLower();
|
||||
if (q == "hello")
|
||||
return QDBusVariant("World");
|
||||
if (q == "ping")
|
||||
return QDBusVariant("Pong");
|
||||
if (q.indexOf("the answer to life, the universe and everything") != -1)
|
||||
return QDBusVariant(42);
|
||||
if (q.indexOf("unladen swallow") != -1) {
|
||||
if (q.indexOf("european") != -1)
|
||||
return QDBusVariant(11.0);
|
||||
return QDBusVariant(QByteArray("african or european?"));
|
||||
}
|
||||
|
||||
return QDBusVariant("Sorry, I don't know the answer");
|
||||
}
|
||||
|
||||
int main(int argc, char **argv)
|
||||
{
|
||||
QCoreApplication app(argc, argv);
|
||||
|
||||
QObject obj;
|
||||
Pong *pong = new Pong(&obj);
|
||||
QObject::connect(&app, &QCoreApplication::aboutToQuit, pong, &Pong::aboutToQuit);
|
||||
pong->setProperty("value", "initial value");
|
||||
|
||||
auto connection = QDBusConnection::sessionBus();
|
||||
connection.registerObject("/", &obj);
|
||||
|
||||
if (!connection.registerService(SERVICE_NAME)) {
|
||||
qWarning().noquote() << connection.lastError().message();
|
||||
return 1;
|
||||
}
|
||||
|
||||
app.exec();
|
||||
return 0;
|
||||
}
|
||||
|
33
examples/dbus/complexpingpong/complexpong.h
Normal file
33
examples/dbus/complexpingpong/complexpong.h
Normal file
@ -0,0 +1,33 @@
|
||||
// Copyright (C) 2016 The Qt Company Ltd.
|
||||
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause
|
||||
|
||||
#ifndef COMPLEXPONG_H
|
||||
#define COMPLEXPONG_H
|
||||
|
||||
#include <QtCore/QObject>
|
||||
#include <QtDBus/QDBusAbstractAdaptor>
|
||||
#include <QtDBus/QDBusVariant>
|
||||
|
||||
class Pong : public QDBusAbstractAdaptor
|
||||
{
|
||||
Q_OBJECT
|
||||
Q_CLASSINFO("D-Bus Interface", "org.example.QtDBus.ComplexPong.Pong")
|
||||
Q_PROPERTY(QString value READ value WRITE setValue)
|
||||
public:
|
||||
QString value() const;
|
||||
void setValue(const QString &newValue);
|
||||
|
||||
Pong(QObject *obj) : QDBusAbstractAdaptor(obj) { }
|
||||
|
||||
signals:
|
||||
void aboutToQuit();
|
||||
|
||||
public slots:
|
||||
QDBusVariant query(const QString &query);
|
||||
Q_NOREPLY void quit();
|
||||
|
||||
private:
|
||||
QString m_value;
|
||||
};
|
||||
|
||||
#endif
|
8
examples/dbus/complexpingpong/complexpong.pro
Normal file
8
examples/dbus/complexpingpong/complexpong.pro
Normal file
@ -0,0 +1,8 @@
|
||||
QT -= gui
|
||||
QT += dbus
|
||||
|
||||
HEADERS += complexpong.h
|
||||
SOURCES += complexpong.cpp
|
||||
|
||||
target.path = $$[QT_INSTALL_EXAMPLES]/dbus/complexpingpong
|
||||
INSTALLS += target
|
4
examples/dbus/complexpingpong/ping-common.h
Normal file
4
examples/dbus/complexpingpong/ping-common.h
Normal file
@ -0,0 +1,4 @@
|
||||
// Copyright (C) 2016 The Qt Company Ltd.
|
||||
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause
|
||||
|
||||
#define SERVICE_NAME "org.example.QtDBus.PingExample"
|
11
examples/dbus/dbus.pro
Normal file
11
examples/dbus/dbus.pro
Normal file
@ -0,0 +1,11 @@
|
||||
requires(qtHaveModule(dbus))
|
||||
|
||||
TEMPLATE = subdirs
|
||||
SUBDIRS = pingpong
|
||||
|
||||
qtConfig(process): SUBDIRS += complexpingpong
|
||||
|
||||
qtHaveModule(widgets) {
|
||||
SUBDIRS += chat \
|
||||
remotecontrolledcar
|
||||
}
|
BIN
examples/dbus/doc/images/dbus-chat-example.webp
Normal file
BIN
examples/dbus/doc/images/dbus-chat-example.webp
Normal file
Binary file not shown.
After Width: | Height: | Size: 17 KiB |
17
examples/dbus/doc/src/chat.qdoc
Normal file
17
examples/dbus/doc/src/chat.qdoc
Normal file
@ -0,0 +1,17 @@
|
||||
// Copyright (C) 2016 The Qt Company Ltd.
|
||||
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GFDL-1.3-no-invariants-only
|
||||
|
||||
/*!
|
||||
\example chat
|
||||
\title D-Bus Chat
|
||||
\ingroup examples-dbus
|
||||
\brief Demonstrates communication among instances of an application.
|
||||
|
||||
\e Chat is a \l{Qt D-Bus} example which demonstrates a simple chat system
|
||||
among instances of an application. Users connect and send message to
|
||||
each other.
|
||||
|
||||
\image dbus-chat-example.webp
|
||||
|
||||
\include examples-run.qdocinc
|
||||
*/
|
27
examples/dbus/doc/src/complexpingpong.qdoc
Normal file
27
examples/dbus/doc/src/complexpingpong.qdoc
Normal file
@ -0,0 +1,27 @@
|
||||
// Copyright (C) 2016 The Qt Company Ltd.
|
||||
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GFDL-1.3-no-invariants-only
|
||||
|
||||
/*!
|
||||
\example complexpingpong
|
||||
\title D-Bus Complex Ping Pong
|
||||
\ingroup examples-dbus
|
||||
\brief Demonstrates usage of the Qt D-Bus typesystem.
|
||||
|
||||
\e{Complex Ping Pong} example demonstrates the use of \l{Qt D-Bus}
|
||||
typesystem with QDBusVariant and QDBusReply. The example consists of the
|
||||
main application \c complexping which starts the other application, \c
|
||||
complexpong. Entering keywords such as \c hello and \c ping is handled by
|
||||
complexpong and the reply is printed to the standard output.
|
||||
|
||||
\include examples-run.qdocinc
|
||||
|
||||
To run, execute the \c complexping application.
|
||||
|
||||
\badcode
|
||||
$ ./complexping
|
||||
Ask your question: When is the next Qt release?
|
||||
Reply was: Sorry, I don't know the answer
|
||||
Ask your question: What is the answer to life, the universe and everything?
|
||||
Reply was: 42
|
||||
\endcode
|
||||
*/
|
24
examples/dbus/doc/src/pingpong.qdoc
Normal file
24
examples/dbus/doc/src/pingpong.qdoc
Normal file
@ -0,0 +1,24 @@
|
||||
// Copyright (C) 2016 The Qt Company Ltd.
|
||||
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GFDL-1.3-no-invariants-only
|
||||
|
||||
/*!
|
||||
\example pingpong
|
||||
\title D-Bus Ping Pong
|
||||
\ingroup examples-dbus
|
||||
\brief Demonstrates a simple message system using D-Bus.
|
||||
|
||||
\e{Ping Pong} is a command-line example that demonstrates the basics of
|
||||
\l{Qt D-Bus}. A message is sent to another application and there is a
|
||||
confirmation of the message.
|
||||
|
||||
\include examples-run.qdocinc
|
||||
|
||||
Run the \c pong application and run the \c ping application with the message
|
||||
as the argument.
|
||||
|
||||
\badcode
|
||||
$ ./pong &
|
||||
$ ./ping Hello
|
||||
Reply was: ping("Hello") got called
|
||||
\endcode
|
||||
*/
|
41
examples/dbus/pingpong/CMakeLists.txt
Normal file
41
examples/dbus/pingpong/CMakeLists.txt
Normal file
@ -0,0 +1,41 @@
|
||||
# Copyright (C) 2022 The Qt Company Ltd.
|
||||
# SPDX-License-Identifier: BSD-3-Clause
|
||||
|
||||
cmake_minimum_required(VERSION 3.16)
|
||||
project(pingpong LANGUAGES CXX)
|
||||
|
||||
if(NOT DEFINED INSTALL_EXAMPLESDIR)
|
||||
set(INSTALL_EXAMPLESDIR "examples")
|
||||
endif()
|
||||
|
||||
set(INSTALL_EXAMPLEDIR "${INSTALL_EXAMPLESDIR}/dbus/pingpong")
|
||||
|
||||
find_package(Qt6 REQUIRED COMPONENTS Core DBus)
|
||||
|
||||
qt_standard_project_setup()
|
||||
|
||||
qt_add_executable(ping
|
||||
ping.cpp
|
||||
ping-common.h
|
||||
)
|
||||
|
||||
target_link_libraries(ping PRIVATE
|
||||
Qt6::Core
|
||||
Qt6::DBus
|
||||
)
|
||||
|
||||
qt_add_executable(pong
|
||||
ping-common.h
|
||||
pong.cpp
|
||||
)
|
||||
|
||||
target_link_libraries(pong PRIVATE
|
||||
Qt6::Core
|
||||
Qt6::DBus
|
||||
)
|
||||
|
||||
install(TARGETS ping pong
|
||||
RUNTIME DESTINATION "${INSTALL_EXAMPLEDIR}"
|
||||
BUNDLE DESTINATION "${INSTALL_EXAMPLEDIR}"
|
||||
LIBRARY DESTINATION "${INSTALL_EXAMPLEDIR}"
|
||||
)
|
4
examples/dbus/pingpong/ping-common.h
Normal file
4
examples/dbus/pingpong/ping-common.h
Normal file
@ -0,0 +1,4 @@
|
||||
// Copyright (C) 2016 The Qt Company Ltd.
|
||||
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause
|
||||
|
||||
#define SERVICE_NAME "org.example.QtDBus.PingExample"
|
40
examples/dbus/pingpong/ping.cpp
Normal file
40
examples/dbus/pingpong/ping.cpp
Normal file
@ -0,0 +1,40 @@
|
||||
// Copyright (C) 2016 The Qt Company Ltd.
|
||||
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause
|
||||
|
||||
#include "ping-common.h"
|
||||
|
||||
#include <QCoreApplication>
|
||||
#include <QDBusConnection>
|
||||
#include <QDBusInterface>
|
||||
#include <QDBusReply>
|
||||
|
||||
#include <iostream>
|
||||
|
||||
int main(int argc, char **argv)
|
||||
{
|
||||
QCoreApplication app(argc, argv);
|
||||
|
||||
auto connection = QDBusConnection::sessionBus();
|
||||
|
||||
if (!connection.isConnected()) {
|
||||
qWarning("Cannot connect to the D-Bus session bus.\n"
|
||||
"To start it, run:\n"
|
||||
"\teval `dbus-launch --auto-syntax`\n");
|
||||
return 1;
|
||||
}
|
||||
|
||||
QDBusInterface iface(SERVICE_NAME, "/");
|
||||
if (iface.isValid()) {
|
||||
QDBusReply<QString> reply = iface.call("ping", argc > 1 ? argv[1] : "");
|
||||
if (reply.isValid()) {
|
||||
std::cout << "Reply was: " << qPrintable(reply.value()) << std::endl;
|
||||
return 0;
|
||||
}
|
||||
|
||||
qWarning("Call failed: %s\n", qPrintable(reply.error().message()));
|
||||
return 1;
|
||||
}
|
||||
|
||||
qWarning("%s\n", qPrintable(connection.lastError().message()));
|
||||
return 1;
|
||||
}
|
8
examples/dbus/pingpong/ping.pro
Normal file
8
examples/dbus/pingpong/ping.pro
Normal file
@ -0,0 +1,8 @@
|
||||
QT -= gui
|
||||
QT += dbus
|
||||
|
||||
HEADERS += ping-common.h
|
||||
SOURCES += ping.cpp
|
||||
|
||||
target.path = $$[QT_INSTALL_EXAMPLES]/dbus/pingpong
|
||||
INSTALLS += target
|
3
examples/dbus/pingpong/pingpong.pro
Normal file
3
examples/dbus/pingpong/pingpong.pro
Normal file
@ -0,0 +1,3 @@
|
||||
TEMPLATE = subdirs
|
||||
win32:CONFIG += console
|
||||
SUBDIRS = ping.pro pong.pro
|
49
examples/dbus/pingpong/pong.cpp
Normal file
49
examples/dbus/pingpong/pong.cpp
Normal file
@ -0,0 +1,49 @@
|
||||
// Copyright (C) 2016 The Qt Company Ltd.
|
||||
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause
|
||||
|
||||
#include "ping-common.h"
|
||||
|
||||
#include <QObject>
|
||||
#include <QCoreApplication>
|
||||
#include <QDBusConnection>
|
||||
#include <QDBusError>
|
||||
|
||||
class Pong : public QObject
|
||||
{
|
||||
Q_OBJECT
|
||||
public slots:
|
||||
QString ping(const QString &arg);
|
||||
};
|
||||
|
||||
QString Pong::ping(const QString &arg)
|
||||
{
|
||||
QMetaObject::invokeMethod(QCoreApplication::instance(), &QCoreApplication::quit);
|
||||
return QString("ping(\"%1\") got called").arg(arg);
|
||||
}
|
||||
|
||||
int main(int argc, char **argv)
|
||||
{
|
||||
QCoreApplication app(argc, argv);
|
||||
|
||||
auto connection = QDBusConnection::sessionBus();
|
||||
|
||||
if (!connection.isConnected()) {
|
||||
qWarning("Cannot connect to the D-Bus session bus.\n"
|
||||
"To start it, run:\n"
|
||||
"\teval `dbus-launch --auto-syntax`\n");
|
||||
return 1;
|
||||
}
|
||||
|
||||
if (!connection.registerService(SERVICE_NAME)) {
|
||||
qWarning("%s\n", qPrintable(connection.lastError().message()));
|
||||
exit(1);
|
||||
}
|
||||
|
||||
Pong pong;
|
||||
connection.registerObject("/", &pong, QDBusConnection::ExportAllSlots);
|
||||
|
||||
app.exec();
|
||||
return 0;
|
||||
}
|
||||
|
||||
#include "pong.moc"
|
8
examples/dbus/pingpong/pong.pro
Normal file
8
examples/dbus/pingpong/pong.pro
Normal file
@ -0,0 +1,8 @@
|
||||
QT -= gui
|
||||
QT += dbus
|
||||
|
||||
HEADERS += ping-common.h
|
||||
SOURCES += pong.cpp
|
||||
|
||||
target.path = $$[QT_INSTALL_EXAMPLES]/dbus/pingpong
|
||||
INSTALLS += target
|
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