pingpong example: Modernize the code
Use qWarning()/std::cout instead of C IO functions. Use default parameters for QDBusInterface's constructor. Remove useless Q_SCRIPTABLE. Use type-safe invokeMethod(). Extract a local variable for the used D-Bus connection. Task-number: QTBUG-111366 Pick-to: 6.5 Change-Id: Idc38a7c1dd97d71308d9491193039744c759d6f2 Reviewed-by: Mårten Nordheim <marten.nordheim@qt.io>
This commit is contained in:
parent
38ee9ee849
commit
07b8a5a409
@ -8,32 +8,33 @@
|
||||
#include <QDBusInterface>
|
||||
#include <QDBusReply>
|
||||
|
||||
#include <stdio.h>
|
||||
#include <iostream>
|
||||
|
||||
int main(int argc, char **argv)
|
||||
{
|
||||
QCoreApplication app(argc, argv);
|
||||
|
||||
if (!QDBusConnection::sessionBus().isConnected()) {
|
||||
fprintf(stderr, "Cannot connect to the D-Bus session bus.\n"
|
||||
"To start it, run:\n"
|
||||
"\teval `dbus-launch --auto-syntax`\n");
|
||||
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, "/", "", QDBusConnection::sessionBus());
|
||||
QDBusInterface iface(SERVICE_NAME, "/");
|
||||
if (iface.isValid()) {
|
||||
QDBusReply<QString> reply = iface.call("ping", argc > 1 ? argv[1] : "");
|
||||
if (reply.isValid()) {
|
||||
printf("Reply was: %s\n", qPrintable(reply.value()));
|
||||
std::cout << "Reply was: " << qPrintable(reply.value()) << std::endl;
|
||||
return 0;
|
||||
}
|
||||
|
||||
fprintf(stderr, "Call failed: %s\n", qPrintable(reply.error().message()));
|
||||
qWarning("Call failed: %s\n", qPrintable(reply.error().message()));
|
||||
return 1;
|
||||
}
|
||||
|
||||
fprintf(stderr, "%s\n",
|
||||
qPrintable(QDBusConnection::sessionBus().lastError().message()));
|
||||
qWarning("%s\n", qPrintable(connection.lastError().message()));
|
||||
return 1;
|
||||
}
|
||||
|
@ -7,14 +7,10 @@
|
||||
#include <QCoreApplication>
|
||||
#include <QDBusConnection>
|
||||
#include <QDBusError>
|
||||
#include <QTimer>
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
QString Pong::ping(const QString &arg)
|
||||
{
|
||||
QMetaObject::invokeMethod(QCoreApplication::instance(), "quit");
|
||||
QMetaObject::invokeMethod(QCoreApplication::instance(), &QCoreApplication::quit);
|
||||
return QString("ping(\"%1\") got called").arg(arg);
|
||||
}
|
||||
|
||||
@ -22,21 +18,22 @@ int main(int argc, char **argv)
|
||||
{
|
||||
QCoreApplication app(argc, argv);
|
||||
|
||||
if (!QDBusConnection::sessionBus().isConnected()) {
|
||||
fprintf(stderr, "Cannot connect to the D-Bus session bus.\n"
|
||||
"To start it, run:\n"
|
||||
"\teval `dbus-launch --auto-syntax`\n");
|
||||
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 (!QDBusConnection::sessionBus().registerService(SERVICE_NAME)) {
|
||||
fprintf(stderr, "%s\n",
|
||||
qPrintable(QDBusConnection::sessionBus().lastError().message()));
|
||||
if (!connection.registerService(SERVICE_NAME)) {
|
||||
qWarning("%s\n", qPrintable(connection.lastError().message()));
|
||||
exit(1);
|
||||
}
|
||||
|
||||
Pong pong;
|
||||
QDBusConnection::sessionBus().registerObject("/", &pong, QDBusConnection::ExportAllSlots);
|
||||
connection.registerObject("/", &pong, QDBusConnection::ExportAllSlots);
|
||||
|
||||
app.exec();
|
||||
return 0;
|
||||
|
@ -10,7 +10,7 @@ class Pong: public QObject
|
||||
{
|
||||
Q_OBJECT
|
||||
public slots:
|
||||
Q_SCRIPTABLE QString ping(const QString &arg);
|
||||
QString ping(const QString &arg);
|
||||
};
|
||||
|
||||
#endif
|
||||
|
Loading…
x
Reference in New Issue
Block a user