linux环境Qt定时功能,在Linux上使用QT设置系统时钟

您可以使用dbus与定时守护程序进行交互https://www.freedesktop.org/wiki/Software/systemd/timedated/

设置时间和日期。

Qt提供了一种从xml http://doc.qt.io/qt-5/qdbusxml2cpp.html生成接口代码的方法 。您可以通过内省获得xml。

我不喜欢生成的代码格式,所以我自己编写了接口代码

H:

#ifndef TIMEDATE1SERVICE_H

#define TIMEDATE1SERVICE_H

#include

#include

#include

#include

class Timedate1Interface: public QDBusAbstractInterface

{

Q_OBJECT

Q_PROPERTY(bool CanNTP READ CanNTP)

Q_PROPERTY(bool LocalRTC READ LocalRTC)

Q_PROPERTY(bool NTP READ NTP)

Q_PROPERTY(bool NTPSynchronized READ NTPSynchronized)

Q_PROPERTY(qulonglong RTCTimeUSec READ RTCTimeUSec)

Q_PROPERTY(qulonglong TimeUSec READ TimeUSec)

Q_PROPERTY(QString Timezone READ Timezone)

public:

explicit Timedate1Interface(QObject *parent = nullptr);

bool CanNTP() const;

bool LocalRTC() const;

bool NTP() const;

bool NTPSynchronized() const;

qulonglong RTCTimeUSec() const;

qulonglong TimeUSec() const;

QString Timezone() const;

void SetLocalRTC(bool localRTC, bool fixSystem, bool userInteraction);

void SetNTP(bool useNTP, bool userInteraction);

void SetTime(qlonglong usecUTC, bool relative, bool userInteraction);

void SetTimezone(const QString &timezone, bool userInteraction);

};

#endif // TIMEDATE1SERVICE_H

cpp:

#include "timedate1service.h"

Timedate1Interface::Timedate1Interface(QObject *parent)

: QDBusAbstractInterface("org.freedesktop.timedate1", "/org/freedesktop/timedate1",

"org.freedesktop.timedate1", QDBusConnection::systemBus(), parent)

{

}

bool Timedate1Interface::CanNTP() const

{

return qvariant_cast(property("CanNTP"));

}

bool Timedate1Interface::LocalRTC() const

{

return qvariant_cast(property("LocalRTC"));

}

bool Timedate1Interface::NTP() const

{

return qvariant_cast(property("NTP"));

}

bool Timedate1Interface::NTPSynchronized() const

{

return qvariant_cast(property("NTPSynchronized"));

}

qulonglong Timedate1Interface::RTCTimeUSec() const

{

return qvariant_cast(property("RTCTimeUSec"));

}

qulonglong Timedate1Interface::TimeUSec() const

{

return qvariant_cast(property("TimeUSec"));

}

QString Timedate1Interface::Timezone() const

{

return qvariant_cast(property("Timezone"));

}

void Timedate1Interface::SetLocalRTC(bool localRTC, bool fixSystem, bool userInteraction)

{

call("SetLocalRTC", localRTC, fixSystem, userInteraction);

}

void Timedate1Interface::SetNTP(bool useNTP, bool userInteraction)

{

call("SetNTP", useNTP, userInteraction);

}

void Timedate1Interface::SetTime(qlonglong usecUTC, bool relative, bool userInteraction)

{

call("SetTime", usecUTC, relative , userInteraction);

}

void Timedate1Interface::SetTimezone(const QString &timezone, bool userInteraction)

{

call("SetTimezone", timezone, userInteraction);

}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值