【发布时间】:2016-03-18 20:56:23
【问题描述】:
我想更改我的系统时间,如何在 Qt 中更改 Windows 系统时间? 我用这种方式,但是失败了!
#include <QApplication>
#include <iostream>
#include <time.h>
#include <windows.h>
#include <QDateTime>
#include <QDebug>
using namespace std;
bool setDate(int,int,int);
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
qDebug()<<QDateTime::currentDateTime()<<endl; //before change time
if(setDate(2015,1,1)) //set time
{
qDebug()<<QDateTime::currentDateTime()<<endl; //if succeed,output time
}
return a.exec();
}
bool setDate(int year,int mon,int day)
{
SYSTEMTIME st;
GetSystemTime(&st); // Win32 API get time
st.wYear=year; //set year
st.wMonth=mon; //set month
st.wDay=day; //set day
return SetSystemTime(&st); //Win32 API set time
}
提前谢谢你。
【问题讨论】:
-
至少
if(setDate(2015,1,1);)->if(setDate(2015,1,1))才能编译。 -
尝试通过
GetLastErrorfunction检查失败原因。 -
是的,我收到一个错误:1314。客户未拥有所需的特权。
-
根据System Error Codes (1300-1699) (Windows)表示
ERROR_PRIVILEGE_NOT_HELD。确保您具有所需的权限。尝试以管理员身份执行程序。 -
知道了。但是如何在Qt中获取系统管理员权限呢?我的系统是Windows8.1。
标签: c++ windows qt winapi time