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:
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"
|
Reference in New Issue
Block a user