zoukankan      html  css  js  c++  java
  • Qt设置系统时间(使用SetSystemTime API函数)

    大家都知道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 

    http://www.cnblogs.com/hicjiajia/archive/2010/08/27/1810363.html

  • 相关阅读:
    jsp上传下载+SmartUpload插件上传
    《鸟哥的Linux私房菜-基础学习篇(第三版)》(五)
    Activity的启动模式
    重学C++ (十一) OOP面向对象编程(2)
    寒城攻略:Listo 教你用 Swift 写IOS UI 项目计算器
    freemarker写select组件报错总结(二)
    用Radeon RAMDisk在Windows 10中创建关机或重新启动不消失的内存虚拟盘
    JS推断是否为JSON对象及是否存在某字段
    json、js数组真心不是想得那么简单
    javascript正則表達式
  • 原文地址:https://www.cnblogs.com/findumars/p/6209716.html
Copyright © 2011-2022 走看看