Qt设置系统时间

大家都知道Qt中有QDateTime等有关时间与日期的类,类中包含很多成员函数,可以很方便的实现有关时间与日期的操作,比如:想要获得系统当前的时间与日期,可以调用currentDateTime(); 但是Qt并没有提供设置系统时间的方法,这样我们只能自己来区分平台,调用平台相关的API,这篇文章实现在Windows下的设置。

常用的与时间有关的Win32 API有两个:GetSystemTime();  与  SetSystemTime();  下面是函数原型:

VOID GetSystemTime(LPSYSTEMTIME lpSystemTime);
BOOL SetSystemTime( 
const  SYSTEMTIME  * lpSystemTime ); 

 我们查一下 MSDN 看看 LPSYSTEMTIME 与 SYSTEMTIME 是什么东东:

typedef  struct  _SYSTEMTIME {
    WORD wYear;
    WORD wMonth;
    WORD wDayOfWeek;
    WORD wDay;
    WORD wHour;
    WORD wMinute;
    WORD wSecond;
    WORD wMilliseconds;
} SYSTEMTIME, 
* PSYSTEMTIME; 

 从中我们知道 SYSTEMTIME 为结构体类型,LPSYSTEMTIME为结构体指针,传递给两个函数的参数都必须是指针或引用类型,下面看一个Qt的调用实例:

 1  #include  < QtCore / QCoreApplication >
 2  #include  < iostream >
 3  #include  < time.h >
 4  #include  < windows.h >
 5  #include  < QDateTime >
 6  #include  < QDebug >
 7  using   namespace  std;
 8 
 9  bool  setDate( int , int , int );
10  int  main( int  argc,  char   * argv[])
11  {
12      QCoreApplication a(argc, argv);
13      qDebug() << QDateTime::currentDateTime() << endl;   // Qt API 输出当前时间
14      setDate(2011,1,1);                             //设置时间
15      qDebug() << QDateTime::currentDateTime() << endl;   // Qt API 获取当前时间
16      return  0;   //让程序完成任务直接退出吧...
17  }
18 
19  bool  setDate( int  year, int  mon, int  day)
20  {
21      SYSTEMTIME st;     
22      GetSystemTime( & st);     // Win32 API 获取系统当前时间,并存入结构体st中
23      st.wYear = year;
24      st.wMonth = mon;
25      st.wDay = day;
26 
27      return  SetSystemTime( & st);     //Win32 API 设置系统时间
28  }
29 

转载于:https://www.cnblogs.com/hicjiajia/archive/2010/08/27/1810363.html

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值