QT学习:QTime类
生活随笔
收集整理的這篇文章主要介紹了
QT学习:QTime类
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
QTime的currentTime():用于獲取當前的系統時間;
QTime 的toString():用于將獲取的當前時間轉換為字符串類型。
為了便于顯示,toString()函數的參數需指定轉換后時間的顯示格式。
顯示格式有如下幾種:
(1)H/h: 小時(若使用H表示小時,則無論何時都以24小時制顯示小時;若使用h表示小時,則當同時指定AM/PM時,采用12 小時制顯示小時,其他情況下仍采用24小時制進行顯示)
(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);//設置窗體的顏色setPalette(p);//將調色板得以運用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函數的內容為:
#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(); }展示的結果如下圖所示:
總結
以上是生活随笔為你收集整理的QT学习:QTime类的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: QT学习:调色板
- 下一篇: QT学习:认识QMainWindow