C++ Qt设置系统时间

设置系统时间,需要包含头文件#include<windows.h>。

bool MainWindow::setPCSystemTime(const QDateTime &dateTime)
{
    SYSTEMTIME   st;
    GetLocalTime(&st);
    st.wYear = dateTime.date().year();
    st.wMonth = dateTime.date().month();
    st.wDay = dateTime.date().day();
    st.wHour = dateTime.time().hour();
    st.wMinute = dateTime.time().minute();
    st.wSecond = dateTime.time().second();
    st.wMilliseconds = dateTime.time().msec();
    return SetLocalTime(&st);

}

注意:

1.GetLocalTime()和GetSystemTime()时间相差8小时。

2.必须以管理员身份运行,否则SetLocalTime()会返回false。

发布了16 篇原创文章 · 获赞 22 · 访问量 921

猜你喜欢

转载自blog.csdn.net/weixin_41882459/article/details/103906040