我试图使用QtDbus与系统中的PowerManager提供的接口进行通信。我的目标很简单。我将是编写代码,使我的系统使用DBus接口进行hibernate。
因此,我安装了d-应用程序,以查看系统上有哪些接口DBus,以及我看到了什么:

正如我们所看到的,我有一些接口和方法,可以从中选择一些东西。我选择的是来自接口Hibernate(),的org.freedesktop.PowerManagment
在这个目标中,我准备了一些非常简单的代码来理解机制。当然,我使用了Qt库:
#include <QtCore/QCoreApplication>
#include <QtCore/QDebug>
#include <QtCore/QStringList>
#include <QtDBus/QtDBus>
#include <QDBusInterface>
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");
return 1;
}
QDBusInterface iface("org.freedesktop.PowerManagement" ,"/" , "" , QDBusConnection::sessionBus());
if(iface.isValid())
{
qDebug() << "Is good";
QDBusReply<QString> reply = iface.call("Methods" , "Hibernate");
if(reply.isValid())
{
qDebug() << "Hibernate by by " << qPrintable(reply.value());
}
qDebug() << "some error " << qPrintable(reply.error().message());
}
return 0;
}不幸的是,我的终端出现了错误:
是好的 一些错误方法“方法”与签名的“on接口”(Null)不存在。
所以请告诉我这个密码有什么问题吗?我肯定我忘记了函数QDBusInterface::call()中的一些论点,但是什么?
发布于 2015-08-26 13:48:14
在创建接口时,必须指定正确的interface, path, service。这就是为什么应该像这样创建iface对象的原因:
QDBusInterface iface("org.freedesktop.PowerManagement", // from list on left
"/org/freedesktop/PowerManagement", // from first line of screenshot
"org.freedesktop.PowerManagement", // from above Methods
QDBusConnection::sessionBus());此外,在调用方法时,需要使用它的名称和参数(如果有的话):
iface.call("Hibernate");而且Hibernate()没有输出参数,所以您必须使用QDBusReply<void>,并且不能检查.value()
QDBusReply<void> reply = iface.call("Hibernate");
if(reply.isValid())
{
// reply.value() is not valid here
}https://stackoverflow.com/questions/32226926
复制相似问题