1

我正在尝试使用QtDbusPowerManager与我系统中提供的接口进行通信。我的目标很简单。我将编写代码使我的系统使用 DBus 接口休眠。

所以,我安装了d-feet应用程序来查看我的系统上有哪些接口 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;
}

不幸的是,我的终端出现错误:

接口“(null)”上带有签名“s”的
一些错误方法“方法”不存在

所以请告诉我这段代码有什么问题?我确定我忘记了函数QDBusInterface::call()中的一些参数,但是什么?

4

1 回答 1

3

创建界面时,您必须指定正确的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
}
于 2015-08-26T13:48:14.720 回答