Qt中时间函数的使用.doc_第1页
Qt中时间函数的使用.doc_第2页
Qt中时间函数的使用.doc_第3页
Qt中时间函数的使用.doc_第4页
Qt中时间函数的使用.doc_第5页
已阅读5页,还剩20页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

QTime:QTime()默认构造函数,构造一个时,分,秒都为0的时间,如00:00:00.000(午夜)QTime:QTime(int h, int m, int s=0, int ms = 0)构造一个用户指定时,分,秒的时间.其参数有效值为:h:0-23m:0-59ms:0-999QTime QTime:addMSecs(int ms) const返回一个当前时间对象之后或之前ms毫秒的时间对象(之前还是之后视ms的符号,如为正则之后,反之之前)如:QTime time(3,0,0);QTime newTime1 = time.addMSecs(1000);QTime newTime2 = time.addMSecs(-1000);则newTime1是一个比time所指定时间(03:00:00.000)延后1000毫秒也即1秒的时间(03:00:01.000),而newTime2则提前1000毫秒(02:59:59.000)QTime QTime:addSecs(int nsecs) const与addMSecs()相同,只是nsecs单位是秒.即返回一个当前时间对象之前或之后的时间对象.int QTime:elapsed() const返回最后一次调用start()或restart()到现在已经经过的毫秒数.如果经过了24小时之后,则计数器置0.如果系统时间设置改变,则结果不确定.int QTime:hour() const返回时间对象的小时,取值范围(0-23)int QTime:minute() const返回时间对象的分钟,取值范围(0-59)int QTime:second() const返回时间对象的秒,取值范围(0-59)int QTime:msec() const返回时间对象的毫秒,取值范围(0-999)bool QTime:isNull() const如果时间对象等于00:00:00.000,则返回true;反之返回false.bool QTime:isValid() const如果时间对象是有效的,则返回true;反之返回false.(即:时,分,秒,毫秒都在其取值范围之内)int QTime:msecsTo(const QTime &t) const返回当前时间对象到t所指定的时间之间的毫秒数.如果t早于当前时间对象的时间,则返回的值是负值.因为一天的时间是86400000毫秒,所以返回值范围是-86400000-86400000int QTime:secsTo(const QTime &t) const与msecsTo()基本相同,只是返回的是秒数,返回值的有效范围是-86400-86400int QTime:restart()设置当前时间对象的值为当前系统时间,并且返回从最后一次调用start()或restart()到现在的毫秒数.如果计数器超出24小时,则置0.如果计数器计数时系统时间设置改变,则结果不确定.bool QTime:setHMS(int h, int m, int s, int ms = 0)设置当前时间对象的时,分,秒和毫秒.如果给定的参数值有效,则返回true,否则返回false.void QTime:start()设置当前时间对象的值为当前系统时间,这个函数实际是结合restart()和elapsed()用来计数的.QString QTime:toString(const QString &format) const按照参数format指定的格式用字符串形式输出当前时间对象的时间.参数format用来指定时,分,秒,毫秒的输出格式.如(hh:mm:ss.zzz)h:表示小时,范围是0-23hh:用两位数表示小时,不足两位的前面用0补足,如(0点:00,3点:03,11点:11)m:表示分钟,范围0-59mm:用两位数表示分钟,不足两位的前面用0补足.s:表示秒,范围0-59ss:用两位数表示秒,不足两位的前面用0补足.z:表示毫秒,范围0-999zzz:用三位数表示毫秒,不足三位的前面用0补足.AP:用AM/PM显示.ap:用ap/pm显示.例如:QTime time(14,3,9,42);/设置时间为14:03:09.042QString i = time.toString(hh:mm:ss.zzz);/结果为14:03:09.042QString j = time.toString(h:m:s.z);/结果为14:3:9.42QString m = time.toString(h:m:s.z AP);/结果为2:3:9.42 PMQString n = time.toString(h:m:s.z ap);/结果为2:3:9.42 pmQString QTime:toString(Qt:DateFormat f = Qt:TextDate) const按照参数format指定的格式用字符串形式输出当前时间对象的时间.参数的可选值:Qt:TextDate:格式为HH:MM:SSQt:ISODate:遵循ISO8601的时间表示格式,同样也为HH:MM:SSQt:LocalDate:字符串格式依赖系统本地设置-静态成员函数:QTime QTime:currentTime()返回当前的系统时间.QTime QTime:fromString(const QString &string, Qt:DateFormat format = Qt:TextDate)使用参数format指定的格式根据参数string指定的时间返回一个时间对象。如果string指定的时间不合法,则返回一个无效的时间对象。format可选值:Qt:TextDate:格式为HH:MM:SSQt:ISODate:遵循ISO8601的时间表示格式,同样也为HH:MM:SSQt:LocalDate:字符串格式依赖系统本地设置QTime QTime:fromString(const QString &string, const QString &format)使用参数format指定的格式根据参数string指定的时间返回一个时间对象.如果string指定的时间不合法,则返回一个无效的时间对象.format的格式参看QString QTime:toString(const QString &format) const.bool QTime:isValid(int h, int m, int s, int ms = 0)如果参数所指定的时间是合法的,则返回true;反之返回false.-静态成员函数不依赖于对象,可以通过类直接调用,与对象无关:如:获取当前系统时间的小时部分时不需要定义QTime对象int hour = QTime:currentTime().hour()QTime类参考QTime类提供了时钟时间功能。 详情请见 #include 所有成员函数的列表。 公有成员 QTime () QTime ( int h, int m, int s = 0, int ms = 0 ) bool isNull () const bool isValid () const int hour () const int minute () const int second () const int msec () const QString toString ( Qt:DateFormat f = Qt:TextDate ) const QString toString ( const QString & format ) const bool setHMS ( int h, int m, int s, int ms = 0 ) QTime addSecs ( int nsecs ) const int secsTo ( const QTime & t ) const QTime addMSecs ( int ms ) const int msecsTo ( const QTime & t ) const bool operator= ( const QTime & t ) const bool operator!= ( const QTime & t ) const bool operator ( const QTime & t ) const bool operator ( const QTime & t ) const bool operator= ( const QTime & t ) const void start () int restart () int elapsed () const静态公有成员 QTime currentTime () QTime fromString ( const QString & s, Qt:DateFormat f = Qt:TextDate ) bool isValid ( int h, int m, int s, int ms = 0 )相关函数 QDataStream & operator ( QDataStream & s, QTime & t )详细描述QTime类提供了时钟时间功能。 QTime对象包含时钟时间,比如从午夜开始的时、分、秒和毫秒数。它可以从系统时钟中读取当前的时间并且度量时间的跨度。它提供比较时间和操作时间的函数,比如加上一定的秒或毫秒。 QTime操作的是24小时时钟格式,它没有AM/PM概念。它操作的是本地时间,它不知道有关时区或白天时间的概念。 QTime对象通常可以由明白地给定的时、分、秒和毫秒数字来创建,或者使用静态函数currentTime()让QTime对象包含系统时钟时间。注意精确性取决于下面操作系统的精确性,不是所有的操作系统都有毫秒级精确度的。 hour()、minute()、second()和msec()函数提供了对时、分、秒和毫秒数字的访问。toString()提供了文本格式的相同信息。 QTime提供了一整套的操作符来比较两个QTime对象,在这里小于表示早一些,大于表示晚一些。 一个给定时间之后给定的秒或毫秒数的时间可以使用addSecs()或addMSecs()得到。相对地,两个时间的秒(或毫秒)数可以使用secsTo()或msecsTo()得到。 QTime可以使用start()、restart()和elapsed()函数度量流逝的时间。 也可以参考QDate、QDateTime和时间和日期。 成员函数文档QTime:QTime () 构造一个时、分、秒和毫秒都是0的时间,比如,00:00:00.000(午夜)。这是一个有效的时间。 也可以参考isValid()。 QTime:QTime ( int h, int m, int s = 0, int ms = 0 ) 构造一个时、分、秒和毫秒分别为h、m、s和ms的时间。 h必须在023之间,m和s必须在059之间,ms必须在0999之间。 也可以参考isValid()。 QTime QTime:addMSecs ( int ms ) const 返回这个时间对象ms毫秒之后的一个时间对象(或者ms毫秒之前的,如果它是一个负数)。 注意如果这个时间过了午夜,它将被转换。请参考addSecs()中的实例。 也可以参考addSecs()和msecsTo()。 QTime QTime:addSecs ( int nsecs ) const 返回这个时间对象nsecs秒之后的一个时间对象(或者nsecs秒之前的,如果它是一个负数)。 注意如果这个时间过了午夜,它将被转换。 实例: QTime n( 14, 0, 0 ); / n = 14:00:00 QTime t; t = n.addSecs( 70 ); / t = 14:01:10 t = n.addSecs( -70 ); / t = 13:58:50 t = n.addSecs( 10*60*60 + 5 ); / t = 00:00:05 t = n.addSecs( -15*60*60 ); / t = 23:00:00 也可以参考addMSecs()、secsTo()和QDateTime:addSecs()。 QTime QTime:currentTime () 静态 返回当前时间,来自于系统时钟。 注意精确性取决于下面操作系统的精确性,不是所有的操作系统都有毫秒级精确度的。 实例:aclock/aclock.cpp、dclock/dclock.cpp、t12/cannon.cpp和tictac/tictac.cpp。 int QTime:elapsed () const 返回从最后一次调用start()或restart()到现在已经过去的毫秒数。 注意如果在最后一次调用start()或restart()后24小时,计数器转换为0。 注意精确性取决于下面操作系统的精确性,不是所有的操作系统都有毫秒级精确度的。 警告: 如果最后一次调用start()或restart()之后,系统时钟设置发生了改变,结果将是不确定的。 也可以参考start()和restart()。 QTime QTime:fromString ( const QString & s, Qt:DateFormat f = Qt:TextDate ) 静态 通过给定的字符串s,使用格式f,返回这个QTime,或者如果这是不可能的话返回一个无效的时间。 注意Qt:LocalDate在这里不能使用。 int QTime:hour () const 返回时间的小时部分(023)。 实例:tictac/tictac.cpp。 bool QTime:isNull () const 如果时间和00:00:00.000相等,返回真,否则返回假。零时间是有效的。 也可以参考isValid()。 bool QTime:isValid () const 如果时间是有效的,返回真,否则返回假。时间23:30:55.746是有效的,而24:12:30是无效的。 也可以参考isNull()。 bool QTime:isValid ( int h, int m, int s, int ms = 0 ) 静态 这是一个重载成员函数,提供了方便。它的行为基本上和上面的函数相同。 如果时间是有效的,返回真,否则返回假。 如果h在023之间、m和s在059之间、ms在0999之间,时间是有效的。 实例: QTime:isValid(21, 10, 30); / 返回真 QTime:isValid(22, 5, 62); / 返回假 int QTime:minute () const 返回时间的分钟部分(059)。 实例:aclock/aclock.cpp和tictac/tictac.cpp。 int QTime:msec () const 返回时间的毫秒部分(0999)。 int QTime:msecsTo ( const QTime & t ) const 返回这个时间到t的毫秒数(如果t早于这个时间,返回的为负数)。 因为QTime只能度量一天之内的时间,而且一天内只有86400000毫秒,所以结果就应该在-86400000毫秒和86400000毫秒之间。 也可以参考secsTo()。 bool QTime:operator!= ( const QTime & t ) const 如果这个时间不等于t,返回真,否则返回假。 bool QTime:operator ( const QTime & t ) const 如果这个时间早于t,返回真,否则返回假。 bool QTime:operator ( const QTime & t ) const 如果这个时间晚于t,返回真,否则返回假。 bool QTime:operator= ( const QTime & t ) const 如果这个时间不早于t,返回真,否则返回假。 int QTime:restart () 设置这个时间为当前时间并且返回从最后一次调用start()或restart()到现在过去的毫秒数。 这个函数保证是原子的并且这样对于重复度量是非常方便的。调用start()开始第一次度量,然后调用restart()来做以后的每一次度量。 注意如果在最后一次调用start()或restart()后24小时,计数器转换为0。 警告: 如果最后一次调用start()或restart()之后,系统时钟设置发生了改变,结果将是不确定的。 也可以参考start()、elapsed()和currentTime()。 int QTime:second () const 返回时间的秒部分(059)。 实例:tictac/tictac.cpp。 int QTime:secsTo ( const QTime & t ) const 返回这个时间到t的秒数(如果t早于这个时间,返回的为负数)。 因为QTime只能度量一天之内的时间,而且一天内只有86400秒,所以结果就应该在-86400秒和86400秒之间。 也可以参考addSecs() and QDateTime:secsTo(). 实例:t12/cannon.cpp。 bool QTime:setHMS ( int h, int m, int s, int ms = 0 ) 设置时间的时、分、秒和毫秒分别为h、m、s和ms。 h必须在023之间,m和s必须在059之间,ms必须在0999之间。如果设置的时间有效,返回真,否则返回假。 也可以参考isValid()。 void QTime:start () 设置这个时间为当前时间。这是实际上使用来计时的: QTime t; t.start(); / 开始计时 . / 一些任务 qDebug( %dn, t.elapsed() ); / 打印过去的毫秒数 也可以参考restart()、elapsed()和currentTime()。 QString QTime:toString ( const QString & format ) const 返回一个字符串的时间。format参数决定了结果字符串的格式。 这些是可能用到的表达式: h - 没有前置0的数字的小时(023或者如果显示AM/PM时,112) hh - 前置0的数字的小时(0023或者如果显示AM/PM时,0112) m - 没有前置0的数字的分钟(059) mm - 前置0的数字的分钟(0059) s - 没有前置0的数字的秒(059) ss - 前置0的数字的秒(0059) z - 没有前置0的数字的毫秒(0999) zzz - 前置0的数字的毫秒(000999) AP - 切换为AM/PM显示。AP将被“AM”或“PM”替换。 ap - 切换为am/pm显示。ap将被“am”或“pm”替换。 所有其他输入字符都将被忽略。 格式字符串实例(假设这个QTime为14:13:09.042) “hh:mm:ss.zzz”的结果将是“14:13:09.042” “h:m:s ap”的结果将是“2:13:9 pm” 也可以参考QDate:toString()和QTime:toString()。 QString QTime:toString ( Qt:DateFormat f = Qt:TextDate ) const 这是一个重载成员函数,提供了方便。它的行为基本上和上面的函数相同。 返回一个字符串的时间。f参数决定了结果字符串的格式。 如果f是Qt:TextDate,字符串格式是HH:MM:SS,比日午夜前一秒是“23:59:59”。 如果f是Qt:ISODate,字符串格式遵循ISO 8601表示时间的说明,也就是HH:MM:SS。 如果f是Qt:LocalDate,字符串格式依赖于系统的本地设置。 相关函数QDataStream & operator ( QDataStream & s, QTime & t ) 从流s中读取一个时间到t中。 也可以参考QDataStream操作符的格式。 QTimer,QTime的一点应用 分类: Qt2008-10-15 00:381114人阅读评论(0)收藏举报QTime可以及时流逝的时间QTimer是“时机”;什么时间发生什么时候,发出一个SIGNAL,执行一个SLOT例子1#include#include#includeint main(int argc,char* argv)QApplication app(argc,argv);std:vector list;QTime tim;tim.start();for(int i=0; i10; i+)Sleep(100);list.push_back(tim.elapsed() );for(quint32 i=0; ilist.size(); i+)printf(%d , list.at(i);printf(/n);return app.exec();则输出为,109 203 312 406 516 609 703 812 906 1016计算一下邻差 94 109 94 110 93 94 109 94 110差不多, 基本维持在100毫秒的延时,这基本说明Sleep的精度,也说明QTime的用法之一。例子2:-timeout.h-#include class TIMEOUT:public QObjectQ_OBJECTprivate:QTime t;public:TIMEOUT()t.start();public slots:void timeout()qDebug(%d , t.elapsed() );-main.cpp-#include timeout.hint main(int argc,char* argv)QApplication app(argc,argv);std:vector list;QTimer timer;timer.start(100);TIMEOUT out;QObject:connect(&timer,SIGNAL(timeout(), &out, SLOT(timeout();for (int i=0; iprocessEvents();return app.exec();输出为 3123284375476567658759841094120313121422153116401750185919692078218722972406251526252734计算一下邻差,16109110109109110109110109109110109109110109110109109110109109110109可见执行Sleep的时候,QTimer是没有机会fire它的signal的;它眼巴巴的等着cpu有空了,才能释放signal;那么是不是释放了signal,但是调度处理没有时机调用slot呢?也有可能吧,但外在的表现是一直的,即来不及处理。When a timer fires, the application sends a QTimerEvent, and the flow of control leaves the event loop until the timer event is processed. This implies that a timer cannot fire while your application is busy doing something else. In other words: the accuracy of timers depends on the granularity of your application. -Qt的assistant上面的例子2中,如果反注释 qApp-processEvents();则输出为15625032843854765676687598510941203131314221531164117501860196920782188邻差为9478110109109110109110109109110109109110109110109109110可见这个qApp-processEvents()的作用了 ,见缝插针,只是第一个sleep和第二个sleep之间没有来得及插针。试图使用QTimer得到固定的,精确的,不依赖于当前任务的时延,是苦难的。QTime Class ReferenceThe QTime class provides clock time functions. More. #include Note: All functions in this class are reentrant. List of all members, including inherited members Qt 3 support membersPublic FunctionsQTime ()QTime ( int h, int m, int s = 0, int ms = 0 )QTime addMSecs ( int ms ) constQTime addSecs ( int s ) constint elapsed () constint hour () constbool isNull () constbool isValid () constint minute () constint msec () constint msecsTo ( const QTime & t ) constint restart ()int second () constint secsTo ( const QTime & t ) constbool setHMS ( int h, int m, int s, int ms = 0 )void start ()QString toString ( const QString & format ) constQString toString ( Qt:DateFormat format = Qt:TextDate ) constbool operator!= ( const QTime & t ) constbool operator ( const QTime & t ) constbool operator ( const QTime & t ) constbool operator= ( const QTime & t ) constStatic Public MembersQTime currentTime ()QTime fromString ( const QString & string, Qt:DateFormat format = Qt:TextDate )QTime fromString ( const QString & string, const QString & format )bool isValid ( int h, int m, int s, int ms = 0 )Related Non-MembersQDataStream & operator ( QDataStream & in, QTime & time )Detailed DescriptionThe QTime class provides clock time functions.A QTime object contains a clock time, i.e. the number of hours, minutes, seconds, and milliseconds since midnight. It can read the current time from the system clock and measure a span of elapsed time. It provides functions for comparing times and for manipulating a time by adding a number of milliseconds.QTime uses the 24-hour clock format; it has no concept of AM/PM. Unlike QDateTime, QTime knows nothing about time zones or daylight savings time (DST).A QTime object is typically created either by giving the number of hours, minutes, seconds, and milliseconds explicitly, or by using the static function currentTime(), which creates a QTime object that contains the systems local time. Note that the accuracy depends on the accuracy of the underlying operating system; not all systems provide 1-millisecond accuracy.The hour(), minute(), second(), and msec() functions provide access to the number of hours, minutes, seconds, and milliseconds of the time. The same information is provided in textual format by the toString() function.QTime provides a full set of operators to compare two QTime objects. One time is considered smaller than another if it is earlier than the other.The time a given number of seconds or milliseconds later than a given time can be found using the addSecs() or addMSecs() functions. Correspondingly, the number of seconds or milliseconds between two times can be found using secsTo() or msecsTo().QTime can be used to measure a span of elapsed time using the start(), restart(), and elapsed() functions.See also QDate and QDateTime.Member Function DocumentationQTime:QTime ()Constructs a null time object. A null time can be a QTime(0, 0, 0, 0) (i.e., midnight) object, except that isNull() returns true and isValid() returns false.See also isNull() and isValid().QTime:QTime ( int h, int m, int s = 0, int ms = 0 )Constructs a time with hour h, minute m, seconds s and milliseconds ms.h must be in the range 0 to 23, m and s must be in the range 0 to 59, and ms must be in the range 0 to 999.See also isValid().QTime QTime:addMSecs ( int ms ) constReturns a QTime object containing a time ms milliseconds later than the time of this object (or earlier if ms is negative).Note that the time will wrap if it passes midnight. See addSecs() for an example.See also addSecs(), msecsTo(), and QDateTime:addMSecs().QTime QTime:addSecs ( int s ) constReturns a QTime object containing a time s seconds later than the time of this object (or earlier if s is negative).Note that the time will wrap if it passes midnight.Example: QTime n(14, 0, 0); / n = 14:00:00 QTime t; t = n.addSecs(70); / t = 14:01:10 t = n.addSecs(-70); / t = 13:58:50 t = n.addSecs(10 * 60 * 60 + 5); / t = 00:00:05 t = n.addSecs(-15 * 60 * 60); / t = 23:00:00See also addMSecs(), secsTo(), and QDateTime:addSecs().QTime QTime:currentTime () staticReturns the current time as reported by the system clock.Note that the accuracy depends on the accuracy of the underlying operating system; not all systems provide 1-millisecond QTime:elapsed () constReturns the number of milliseconds that have elapsed since the last time start() or restart() was called.Note that the counter wraps to zero 24 hours after the last call to start() or restart.Note that the accuracy depends on the accuracy of the underlying operating system; not all systems provide 1-millisecond accuracy.Warning: If the systems clock setting has been changed since the last time start() or restart() was called, the result is undefined. This can happen when daylight savings time is turned on or off.See also start() and restart().QTime QTime:fromString ( const QString & string, Qt:DateFormat format = Qt:TextDate ) staticReturns the time represented in the string as a QTime using the format given, or an invalid time if this is not possible.Note that fromString() uses a C locale encoded string to convert milliseconds to a float value. If the default locale is not C, this may result in two conversion attempts (if the conversion fails for the default locale). This should be considered an implementation detail.QTime QTime:fromString ( const QString & string, const QString & format ) staticReturns the QTime represented by the string,

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论