QT学习:QTime类
生活随笔
收集整理的這篇文章主要介紹了
QT学习:QTime类
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
QTime的currentTime():用于獲取當(dāng)前的系統(tǒng)時間;
QTime 的toString():用于將獲取的當(dāng)前時間轉(zhuǎn)換為字符串類型。
為了便于顯示,toString()函數(shù)的參數(shù)需指定轉(zhuǎn)換后時間的顯示格式。
顯示格式有如下幾種:
(1)H/h: 小時(若使用H表示小時,則無論何時都以24小時制顯示小時;若使用h表示小時,則當(dāng)同時指定AM/PM時,采用12 小時制顯示小時,其他情況下仍采用24小時制進(jìn)行顯示)
(2)m:分
(3)s:秒
(4)AP/A:顯示AM或PM
(5)Ap/a:顯示am或pm
下面以代碼形式介紹具體用法:
頭文件:
cpp文件:
#include "digiclock.h" #include<QMouseEvent>DigiClock::DigiClock(QWidget *parent):QLCDNumber(parent) {QPalette p=palette();p.setColor(QPalette::Window,Qt::blue);//設(shè)置窗體的顏色setPalette(p);//將調(diào)色板得以運(yùn)用setWindowFlags(Qt::FramelessWindowHint);setWindowOpacity(0.5);//showTime();QTimer *timer=new QTimer(this);connect(timer,SIGNAL(timeout()),this,SLOT(showTime()));timer->start(1000);showColon=true;showTime();resize(150,60);//showColon=true;}void DigiClock::showTime() {QTime time=QTime::currentTime();QString text=time.toString("hh:mm");if(showColon){text[2]=':';showColon=false;}else{text[2]=';';showColon=true;}display(text); }void DigiClock::mousePressEvent(QMouseEvent *event) {if(event->button()==Qt::LeftButton){dragPosition=event->globalPos()-frameGeometry().topLeft();event->accept();}if(event->button()==Qt::RightButton){close();} } void DigiClock::mouseMoveEvent(QMouseEvent *event) {if(event->buttons()&Qt::LeftButton){move(event->globalPos()-dragPosition);event->accept();} }main函數(shù)的內(nèi)容為:
#include "dialog.h" #include <QApplication> #include"digiclock.h"int main(int argc, char *argv[]) {QApplication a(argc, argv);DigiClock clock;clock.show();//Dialog w;// w.show();return a.exec(); }展示的結(jié)果如下圖所示:
總結(jié)
以上是生活随笔為你收集整理的QT学习:QTime类的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: QT学习:调色板
- 下一篇: QT学习:认识QMainWindow