Doc: Make dbus snippets compilable

Task-number: QTBUG-84470
Change-Id: Idfe86ae0f38e43678cc5e746e30e5eeaf8eb72dc
Reviewed-by: Topi Reiniö <topi.reinio@qt.io>
Reviewed-by: Nico Vertriest <nico.vertriest@qt.io>
bb10
Nico Vertriest 2020-05-28 15:03:37 +02:00 committed by Topi Reinio
parent c513b1214e
commit 05b60631a2
12 changed files with 327 additions and 60 deletions

View File

@ -47,6 +47,12 @@
** $QT_END_LICENSE$
**
****************************************************************************/
#include <QString>
#include <QDBusMessage>
#include <QDBusConnection>
struct RequestData;
void appendRequest(RequestData *) {}; // stub
//! [10]
struct RequestData
@ -73,7 +79,7 @@ QString processRequest(const QString &request, const QDBusMessage &message)
void sendReply(RequestData *data)
{
// data->processedData has been initialized with the request's reply
QDBusMessage &reply = &data->reply;
QDBusMessage &reply = data->reply;
// send the reply over D-Bus:
reply << data->processedData;

View File

@ -48,6 +48,35 @@
**
****************************************************************************/
#include <QString>
#include <QDBusMessage>
#include <QDBusReply>
#include <QDBusInterface>
class Abstract_DBus_Interface : public QObject
{
Q_OBJECT
public:
Abstract_DBus_Interface(QObject *parent = nullptr)
: QObject(parent) {
interface = new QDBusInterface("org.example.Interface", "/Example/Methods");
}
~Abstract_DBus_Interface() { delete interface; }
void interfaceMain();
void asyncCall();
QString retrieveValue() { return QString(); }
public slots:
void callFinishedSlot();
private:
QDBusInterface *interface;
};
void Abstract_DBus_Interface::interfaceMain()
{
//! [0]
QString value = retrieveValue();
QDBusMessage reply;
@ -58,13 +87,17 @@ if (api >= 14)
else
reply = interface->call(QLatin1String("ProcessWork"), QLatin1String("UTF-8"), value.toUtf8());
//! [0]
}
void Abstract_DBus_Interface::asyncCall()
{
//! [1]
QString value = retrieveValue();
QDBusPendingCall pcall = interface->asyncCall(QLatin1String("Process"), value);
QDBusPendingCallWatcher *watcher = new QDBusPendingCallWatcher(pcall, this);
QDBusPendingCallWatcher *watcher = new QDBusPendingCallWatcher(pcall);
QObject::connect(watcher, SIGNAL(finished(QDBusPendingCallWatcher*)),
this, SLOT(callFinishedSlot(QDBusPendingCallWatcher*)));
this, SLOT(callFinishedSlot(QDBusPendingCallWatcher*)));
//! [1]
}

View File

@ -48,99 +48,205 @@
**
****************************************************************************/
//! [0]
#include <QString>
#include <QDBusArgument>
#include <QDBusMetaType>
#include <QDBusMessage>
#include <QDBusContext>
typedef QDBusVariant MyElement;
typedef QList<MyElement> MyArray;
typedef QHash<int, MyElement> MyDictionary;
typedef QDBusVariant MyType;
typedef QDBusVariant MyValue;
typedef QDBusVariant Type;
QDBusArgument argument;
class MyObject: public QObject
{
Q_OBJECT
Q_CLASSINFO("D-Bus Interface", "org.qtproject.QtDBus.MyObject")
Q_CLASSINFO("D-Bus Introspection", ""
" <interface name=\"org.qtproject.QtDBus.MyObject\" >\n"
" <property access=\"readwrite\" type=\"i\" name=\"prop1\" />\n"
" <property name=\"complexProp\" type=\"ai\" access=\"readwrite\">\n"
" <annotation name=\"org.qtproject.QtDBus.QtTypeName\" value=\"QList&lt;int&gt;\"/>\n"
" </property>\n"
" <signal name=\"somethingHappened\" >\n"
" <arg direction=\"out\" type=\"s\" />\n"
" </signal>\n"
" <method name=\"ping\" >\n"
" <arg direction=\"in\" type=\"v\" name=\"ping\" />\n"
" <arg direction=\"out\" type=\"v\" name=\"ping\" />\n"
" </method>\n"
" <method name=\"ping_invokable\" >\n"
" <arg direction=\"in\" type=\"v\" name=\"ping_invokable\" />\n"
" <arg direction=\"out\" type=\"v\" name=\"ping_invokable\" />\n"
" </method>\n"
" <method name=\"ping\" >\n"
" <arg direction=\"in\" type=\"v\" name=\"ping1\" />\n"
" <arg direction=\"in\" type=\"v\" name=\"ping2\" />\n"
" <arg direction=\"out\" type=\"v\" name=\"pong1\" />\n"
" <arg direction=\"out\" type=\"v\" name=\"pong2\" />\n"
" </method>\n"
" <method name=\"ping_invokable\" >\n"
" <arg direction=\"in\" type=\"v\" name=\"ping1_invokable\" />\n"
" <arg direction=\"in\" type=\"v\" name=\"ping2_invokable\" />\n"
" <arg direction=\"out\" type=\"v\" name=\"pong1_invokable\" />\n"
" <arg direction=\"out\" type=\"v\" name=\"pong2_invokable\" />\n"
" </method>\n"
" <method name=\"ping\" >\n"
" <arg direction=\"in\" type=\"ai\" name=\"ping\" />\n"
" <arg direction=\"out\" type=\"ai\" name=\"ping\" />\n"
" <annotation name=\"org.qtproject.QtDBus.QtTypeName.In0\" value=\"QList&lt;int&gt;\"/>\n"
" <annotation name=\"org.qtproject.QtDBus.QtTypeName.Out0\" value=\"QList&lt;int&gt;\"/>\n"
" </method>\n"
" <method name=\"ping_invokable\" >\n"
" <arg direction=\"in\" type=\"ai\" name=\"ping_invokable\" />\n"
" <arg direction=\"out\" type=\"ai\" name=\"ping_invokable\" />\n"
" <annotation name=\"org.qtproject.QtDBus.QtTypeName.In0\" value=\"QList&lt;int&gt;\"/>\n"
" <annotation name=\"org.qtproject.QtDBus.QtTypeName.Out0\" value=\"QList&lt;int&gt;\"/>\n"
" </method>\n"
" </interface>\n"
"")
Q_PROPERTY(int prop1 READ prop1 WRITE setProp1)
Q_PROPERTY(QList<int> complexProp READ complexProp WRITE setComplexProp)
public:
static int callCount;
static QVariantList callArgs;
MyObject()
{
QObject *subObject = new QObject(this);
subObject->setObjectName("subObject");
}
};
struct MyMember
{
int subMember1;
int subMember2;
};
//! [0-0]
struct MyStructure
{
int count;
QString name;
//! [0-0]
MyMember member1;
MyMember member2;
MyMember member3;
MyMember member4;
//! [0-1]
// ...
};
Q_DECLARE_METATYPE(MyStructure)
// Marshall the MyStructure data into a D-Bus argument
QDBusArgument &operator<<(QDBusArgument &argument, const MyStructure &mystruct)
QDBusArgument &operator<<(QDBusArgument &argument, const MyStructure &myStruct)
{
argument.beginStructure();
argument << mystruct.count << mystruct.name;
argument << myStruct.count << myStruct.name;
argument.endStructure();
return argument;
}
// Retrieve the MyStructure data from the D-Bus argument
const QDBusArgument &operator>>(const QDBusArgument &argument, MyStructure &mystruct)
const QDBusArgument &operator>>(const QDBusArgument &argument, MyStructure &myStruct)
{
argument.beginStructure();
argument >> mystruct.count >> mystruct.name;
argument >> myStruct.count >> myStruct.name;
argument.endStructure();
return argument;
}
//! [0]
//! [0-1]
const QDBusArgument &operator<<(const QDBusArgument &argument, const MyMember &/*member*/)
{
return argument;
}
const QDBusArgument &operator>>(const QDBusArgument &argument, const MyMember &/*member*/)
{
return argument;
}
void registerMyStructure()
{
//! [1]
qDBusRegisterMetaType<MyStructure>();
//! [1]
}
void castType()
{
QVariant argument = MyObject::callArgs.at(0);
QDBusVariant dv = qdbus_cast<QDBusVariant>(argument);
//! [2]
MyType item = qdbus_cast<Type>(argument);
//! [2]
}
void argumentItem()
{
//! [3]
MyType item;
argument >> item;
//! [3]
}
namespace QDBusSnippets
{
//! [4]
QDBusArgument &operator<<(QDBusArgument &argument, const MyStructure &mystruct)
QDBusArgument &operator<<(QDBusArgument &argument, const MyStructure &myStruct)
{
argument.beginStructure();
argument << mystruct.member1 << mystruct.member2 << ... ;
argument << myStruct.member1 << myStruct.member2;
argument.endStructure();
return argument;
}
//! [4]
namespace Alt {
//! [5]
QDBusArgument &operator<<(QDBusArgument &argument, const MyStructure &mystruct)
QDBusArgument &operator<<(QDBusArgument &argument, const MyStructure &myStruct)
{
argument.beginStructure();
argument << mystruct.member1 << mystruct.member2;
argument << myStruct.member1 << myStruct.member2;
argument.beginStructure();
argument << mystruct.member3.subMember1 << mystruct.member3.subMember2;
argument << myStruct.member3.subMember1 << myStruct.member3.subMember2;
argument.endStructure();
argument << mystruct.member4;
argument << myStruct.member4;
argument.endStructure();
return argument;
}
//! [5]
} // namespace
//! [6]
// append an array of MyElement types
QDBusArgument &operator<<(QDBusArgument &argument, const MyArray &myarray)
// Append an array of MyElement types
QDBusArgument &operator<<(QDBusArgument &argument, const MyArray &myArray)
{
argument.beginArray( qMetaTypeId<MyElement>() );
for ( int i = 0; i < myarray.length; ++i )
argument << myarray.elements[i];
argument.beginArray(qMetaTypeId<MyElement>());
for (const auto &element : myArray)
argument << element;
argument.endArray();
return argument;
}
//! [6]
//! [7]
// append a dictionary that associates ints to MyValue types
QDBusArgument &operator<<(QDBusArgument &argument, const MyDictionary &mydict)
// Append a dictionary that associates ints to MyValue types
QDBusArgument &operator<<(QDBusArgument &argument, const MyDictionary &myDict)
{
argument.beginMap( QVariant::Int, qMetaTypeId<MyValue>() );
for ( int i = 0; i < mydict.length; ++i ) {
argument.beginMap(QVariant::Int, qMetaTypeId<MyValue>());
MyDictionary::const_iterator i;
for (i = myDict.cbegin(); i != myDict.cend(); ++i) {
argument.beginMapEntry();
argument << mydict.data[i].key << mydict.data[i].value;
argument << i.key() << i.value();
argument.endMapEntry();
}
argument.endMap();
@ -148,29 +254,27 @@ QDBusArgument &operator<<(QDBusArgument &argument, const MyDictionary &mydict)
}
//! [7]
//! [8]
const QDBusArgument &operator>>(const QDBusArgument &argument, MyStructure &mystruct)
const QDBusArgument &operator>>(const QDBusArgument &argument, MyStructure &myStruct)
{
argument.beginStructure()
argument >> mystruct.member1 >> mystruct.member2 >> mystruct.member3 >> ...;
argument.beginStructure();
argument >> myStruct.member1 >> myStruct.member2 >> myStruct.member3;
argument.endStructure();
return argument;
}
//! [8]
//! [9]
// extract a MyArray array of MyElement elements
const QDBusArgument &operator>>(const QDBusArgument &argument, MyArray &myarray)
// Extract a MyArray array of MyElement elements
const QDBusArgument &operator>>(const QDBusArgument &argument, MyArray &myArray)
{
argument.beginArray();
myarray.clear();
myArray.clear();
while ( !argument.atEnd() ) {
while (!argument.atEnd()) {
MyElement element;
argument >> element;
myarray.append( element );
myArray.append(element);
}
argument.endArray();
@ -178,24 +282,24 @@ const QDBusArgument &operator>>(const QDBusArgument &argument, MyArray &myarray)
}
//! [9]
//! [10]
// extract a MyDictionary map that associates ints to MyValue elements
const QDBusArgument &operator>>(const QDBusArgument &argument, MyDictionary &mydict)
// Extract a MyDictionary map that associates integers to MyElement items
const QDBusArgument &operator>>(const QDBusArgument &argument, MyDictionary &myDict)
{
argument.beginMap();
mydict.clear();
myDict.clear();
while ( !argument.atEnd() ) {
while (!argument.atEnd()) {
int key;
MyValue value;
MyElement value;
argument.beginMapEntry();
argument >> key >> value;
argument.endMapEntry();
mydict.append( key, value );
myDict.insert(key, value);
}
argument.endMap();
return argument;
}
//! [10]
}

View File

@ -48,15 +48,23 @@
**
****************************************************************************/
#include <QString>
#include <QDBusConnection>
#include <QDBusArgument>
#include <QDBusMetaType>
#include <QDBusMessage>
#include <QDBusContext>
//! [0]
class MyObject: public QObject,
protected QDBusContext
{
Q_OBJECT
QDBusConnection conn;
QDBusMessage msg;
...
//...
protected slots:
void process();

View File

@ -47,7 +47,11 @@
** $QT_END_LICENSE$
**
****************************************************************************/
#include <QDBusInterface>
#include <QDBusReply>
void DBusInterface_main()
{
//! [0]
QDBusInterface remoteApp( "com.example.Calculator", "/Calculator/Operations",
"org.mathematics.RPNCalculator" );
@ -59,3 +63,4 @@ QDBusReply<int> reply = remoteApp.call( "PopOperand" );
if ( reply.isValid() )
printf( "%d", reply.value() ); // prints 4
//! [0]
}

View File

@ -1,6 +1,6 @@
/****************************************************************************
**
** Copyright (C) 2016 The Qt Company Ltd.
** Copyright (C) 2020 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
**
** This file is part of the documentation of the Qt Toolkit.
@ -48,7 +48,14 @@
**
****************************************************************************/
//! [0]
#include <QtCore/qlist.h>
typedef QList<QString> MyClass;
//! [0-0]
#include <QDBusMetaType>
//! [0-0]
void dbus() {
//! [0-1]
qDBusRegisterMetaType<MyClass>();
//! [0]
//! [0-1]
}

View File

@ -47,7 +47,34 @@
** $QT_END_LICENSE$
**
****************************************************************************/
#include <QDBusPendingCall>
#include <QDBusInterface>
#include <QDBusPendingReply>
class DBus_PendingCall_Interface : public QObject
{
Q_OBJECT
public:
DBus_PendingCall_Interface(QObject *parent = nullptr)
: QObject(parent) {
iface = new QDBusInterface("org.example.Interface", "/Example/Methods");
}
~DBus_PendingCall_Interface() { delete iface; }
void callInterfaceMain();
void showError();
void showReply(QString&, QByteArray&);
QString value1;
QString value2;
void callFinishedSlot(QDBusPendingCallWatcher *call);
public slots:
private:
QDBusInterface *iface;
};
void DBus_PendingCall_Interface::callInterfaceMain()
{
//! [0]
QDBusPendingCall async = iface->asyncCall("RemoteMethod", value1, value2);
@ -60,7 +87,7 @@
}
//! [1]
void MyClass::callFinishedSlot(QDBusPendingCallWatcher *call)
void DBus_PendingCall_Interface::callFinishedSlot(QDBusPendingCallWatcher *call)
{
QDBusPendingReply<QString, QByteArray> reply = *call;
if (reply.isError()) {

View File

@ -47,21 +47,52 @@
** $QT_END_LICENSE$
**
****************************************************************************/
#include <QDBusPendingCall>
#include <QDBusInterface>
#include <QDBusPendingReply>
class DBus_PendingReply_Interface : public QObject
{
Q_OBJECT
public:
DBus_PendingReply_Interface(QObject *parent = nullptr)
: QObject(parent) {
iface = new QDBusInterface("org.example.Interface", "/Example/Methods");
}
~DBus_PendingReply_Interface() { delete iface; }
void callInterfaceMainR();
void PendingReplyString();
void PendingReplyBool();
void showErrorD(QDBusError);
void showSuccess(QVariant);
void showFailure(QVariant);
void useValue(QDBusPendingReplyTypes::Select<0, QString, void, void, void, void, void, void, void>::Type);
public slots:
private:
QDBusInterface *iface;
};
void DBus_PendingReply_Interface::PendingReplyString()
{
//! [0]
QDBusPendingReply<QString> reply = interface->asyncCall("RemoteMethod");
QDBusPendingReply<QString> reply = iface->asyncCall("RemoteMethod");
reply.waitForFinished();
if (reply.isError())
// call failed. Show an error condition.
showError(reply.error());
showErrorD(reply.error());
else
// use the returned value
useValue(reply.value());
//! [0]
}
void DBus_PendingReply_Interface::PendingReplyBool()
{
//! [2]
QDBusPendingReply<bool, QString> reply = interface->asyncCall("RemoteMethod");
QDBusPendingReply<bool, QString> reply = iface->asyncCall("RemoteMethod");
reply.waitForFinished();
if (!reply.isError()) {
if (reply.argumentAt<0>())

View File

@ -47,7 +47,32 @@
** $QT_END_LICENSE$
**
****************************************************************************/
#include <QDBusPendingCall>
#include <QDBusInterface>
#include <QDBusPendingReply>
#include <QDBusReply>
class DBus_Process_String_Interface : public QObject
{
Q_OBJECT
public:
DBus_Process_String_Interface(QObject *parent = nullptr)
: QObject(parent) {
interface = new QDBusInterface("org.example.Interface", "/Example/Methods");
}
~DBus_Process_String_Interface() { delete interface; }
void QDBus_reply();
void useValue(QVariant);
void showError(const QDBusError&);
public slots:
private:
QDBusInterface *interface;
};
void DBus_Process_String_Interface::QDBus_reply()
{
//! [0]
QDBusReply<QString> reply = interface->call("RemoteMethod");
if (reply.isValid())
@ -60,5 +85,6 @@ else
//! [1]
QString reply = interface->call("RemoteMethod");
reply = interface->call("RemoteMethod");
//! [1]
}

View File

@ -1,3 +1,19 @@
#! [qmake_use]
QT += dbus
#! [qmake_use]
TEMPLATE = lib
TARGET = qtdbus_snippets
QT += core dbus xml
load(qt_common)
SOURCES += code/src_qdbus_qdbusabstractinterface.cpp \
code/src_qdbus_qdbusinterface.cpp \
code/src_qdbus_qdbuspendingcall.cpp \
code/src_qdbus_qdbuspendingreply.cpp \
code/src_qdbus_qdbusreply.cpp \
code/doc_src_qdbusadaptors.cpp \
code/src_qdbus_qdbusargument.cpp \
code/src_qdbus_qdbuscontext.cpp \
code/src_qdbus_qdbusmetatype.cpp

View File

@ -188,7 +188,9 @@ bool QDBusArgumentPrivate::checkReadAndDetach(QDBusArgumentPrivate *&d)
integer and a string can be constructed using the \l
{qdbustypesystem.html}{Qt D-Bus type system}:
\snippet code/src_qdbus_qdbusargument.cpp 0
\snippet code/src_qdbus_qdbusargument.cpp 0-0
\codeline
\snippet code/src_qdbus_qdbusargument.cpp 0-1
The type has to be registered with qDBusRegisterMetaType() before
it can be used with QDBusArgument. Therefore, somewhere in your

View File

@ -175,7 +175,9 @@ Q_GLOBAL_STATIC(QReadWriteLock, customTypesLock)
Q_DECLARE_METATYPE() macro, and then registered as in the
following example:
\snippet code/src_qdbus_qdbusmetatype.cpp 0
\snippet code/src_qdbus_qdbusmetatype.cpp 0-0
\codeline
\snippet code/src_qdbus_qdbusmetatype.cpp 0-1
If \c{T} isn't one of
Qt's \l{container classes}, the \c{operator<<} and