官网资料:
SetLocalTime 函数 (sysinfoapi.h) - Win32 apps | Microsoft Learn
代码:
SYSTEMTIME st;
//GetLocalTime(&st); //获取系统时间
st.wYear = 2023; //年
st.wMonth = 3; //月
st.wDay = 31; //日
st.wHour= 15; //时
st.wMinute = 40; //分
st.wSecond = 0; //秒
if(SetLocalTime(&st)) //反回非0表示更改成功
{
qDebug() << "更改时间成功";
ui->resultLabel->setText("更改时间成功");
}
else
{
qDebug() << "更改时间失败";
ui->resultLabel->setText("更改时间失败");
}
注:
更改不了时间原因:没有SE_SYSTEMTIME_NAME特权
解决办法:
- 使用有管理员用户的账号
- 以管理员身份运行exe可执行文件
- 以管理员身份运行QtCreator