简体   繁体   English

如何在Qt中更改Windows系统时间?

[英]How to change windows system time in Qt?

I want to change my system time ,How can I change the Windows system time in Qt? 我想更改系统时间,如何在Qt中更改Windows系统时间? I used this way,but failed! 我用这种方式,但是失败了!

#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
 }

Thank you in advance. 先感谢您。

Changing the system time requires admin rights. 更改系统时间需要管理员权限。 That means you need to: 这意味着您需要:

  • Add the requireAdministrator option to your manifest so that the program always has admin rights. requireAdministrator选项添加到清单中,以便该程序始终具有管理员权限。 That's a bad idea and you won't enjoy the UAC dialog every time you start. 这是个坏主意,您不会在每次启动时都喜欢UAC对话框。
  • Or, change the time by starting a separate process that runs as administrator. 或者,通过启动以管理员身份运行的单独过程来更改时间。 Another executable with the appropriate manifest, a process started with the runas shell verb, or one started with the COM elevation moniker. 另一个具有适当清单的可执行文件,一个以runas shell动词开头的进程,或者一个以COM高程名字开头的进程。

If this is gobbledygook to you, you need to read up on UAC. 如果您觉得这很麻烦,则需要阅读UAC。 Start here: https://msdn.microsoft.com/en-us/library/windows/desktop/dn742497(v=vs.85).aspx 从这里开始: https : //msdn.microsoft.com/zh-cn/library/windows/desktop/dn742497(v=vs.85).aspx

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM