我尝试通过 DBus使用 Qt 设置系统时间,方法如下:
#include <QDBusConnection>
#include <QDBusInterface>
#include <QDBusMessage>
#include <QDebug>
#include <QDateTime>
#include <cstdlib>
int main (int /*argc*/, char ** /*argv*/)
{
QDBusConnection dbConnection = QDBusConnection::systemBus ();
QDBusInterface dbInterface (
"org.freedesktop.timedate1.set-time"
, "/org/freedesktop/timedate1/set-time/Manager"
, "org.freedesktop.timedate1.set-time.Manager"
, dbConnection);
qDebug () << "DBus interface validation: " << dbInterface.isValid ();
if (dbInterface.isValid () ) {
QDBusMessage dbMessage = dbInterface.call ("SetTime", QDateTime::currentDateTime ().toMSecsSinceEpoch () * 1000, false, false);
qDebug () << "DBus message: " << dbMessage;
}
return EXIT_SUCCESS;
}
但我有:DBus interface validation: false
。
如果我在控制台中调用:
$ gdbus introspect \
--system \
--dest org.freedesktop.timedate1 \
--object-path /org/freedesktop/timedate1
我得到了一些相关的输出(所以看起来环境没有问题):
node /org/freedesktop/timedate1 {
interface org.freedesktop.DBus.Peer {
...
};
interface org.freedesktop.DBus.Introspectable {
...
};
interface org.freedesktop.DBus.Properties {
methods:
...
signals:
...
properties:
};
interface org.freedesktop.timedate1 {
methods:
SetTime(in x arg_0,
in b arg_1,
in b arg_2);
...
signals:
properties:
...
};
};
GitLab提供源代码和构建脚本。