QTimer與QTime實現(xiàn)電子時鐘
本文實例為大家分享了QTimer與QTime實現(xiàn)電子時鐘的具體代碼,供大家參考,具體內容如下
使用QLCDNumber控件進行顯示
QLCDNumber控件默認只顯示5個字符,可以使用setDigitCount(int size)進行設置顯示個數(shù)
使用Display(QString str) 設置顯示內容
該函數(shù)擁有多個重載,字符 整型 浮點型都可以作為參數(shù)
效果圖:
代碼:頭文件
#include <QLCDNumber> class NumClock : public QLCDNumber { Q_OBJECT public: explicit NumClock(QWidget *parent = nullptr); void mousePressEvent(QMouseEvent *event); void mouseMoveEvent(QMouseEvent *event); signals: public slots: void updateTime(); private: QTimer * timer; QPoint mouseOfPonit; // 鼠標坐標跟窗口左上角坐標的偏移值 bool showColon; //是否顯示: };
cpp文件:
#include "numclock.h" #include <QTimer> #include <QTime> #include <QMouseEvent> #include <QDebug> NumClock::NumClock(QWidget *parent) : QLCDNumber(parent) { timer = new QTimer(this); timer->setTimerType(Qt::PreciseTimer); // 設置精度為較高精度,差距在毫秒內 timer->start(1000); connect(timer, SIGNAL(timeout()), this, SLOT(updateTime()),Qt::QueuedConnection); setWindowFlag(Qt::FramelessWindowHint); //沒有面板邊框標題欄的窗體 setWindowOpacity(0.5); //設置窗口的透明度 showColon = true; this->setDigitCount(8); resize(150, 100); updateTime(); setAttribute(Qt::WA_DeleteOnClose); } void NumClock::mousePressEvent(QMouseEvent *event) { if(event->button() == Qt::LeftButton){ mouseOfPonit = event->globalPos() - this->pos(); event->accept(); }else{ close(); } } void NumClock::mouseMoveEvent(QMouseEvent *event) { if(event->buttons() & Qt::LeftButton){ move(event->globalPos() - mouseOfPonit); event->accept(); } } void NumClock::updateTime() { QString timeStr = QTime::currentTime().toString("hh:mm:ss"); if(showColon){ timeStr = timeStr.replace(QString(":"), QString(" ")); qDebug() << timeStr; showColon = false; }else{ timeStr = timeStr.replace(QString(" "), QString(":")); showColon = true; qDebug() << timeStr; } display(timeStr); }
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關文章
C++利用多態(tài)實現(xiàn)職工管理系統(tǒng)(項目開發(fā))
這篇文章主要介紹了C++利用多態(tài)實現(xiàn)職工管理系統(tǒng)(項目開發(fā)),本文通過實例代碼給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2021-01-01C語言靜態(tài)與動態(tài)通訊錄的實現(xiàn)流程詳解
這篇文章主要為大家介紹了C語言分別實現(xiàn)靜態(tài)與動態(tài)的通訊錄示例代碼教程,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2021-11-11詳解C++ STL模擬實現(xiàn)forward_list
forward_list是C++ 11新增的容器,它支持從容器中的任何位置快速插入和移除元素的容器,不支持快速隨機訪問。本文將模擬實現(xiàn)forward_list,感興趣的可以了解一下2023-01-01C++實現(xiàn)LeetCode(904.水果裝入果籃)
這篇文章主要介紹了C++實現(xiàn)LeetCode(904.水果裝入果籃),本篇文章通過簡要的案例,講解了該項技術的了解與使用,以下就是詳細內容,需要的朋友可以參考下2021-07-07實例講解C++設計模式編程中State狀態(tài)模式的運用場景
這篇文章主要介紹了實例講解C++設計模式編程中State狀態(tài)模式的運用場景,文章最后的適用性部分則介紹了一些State模式善于處理的情況,需要的朋友可以參考下2016-03-03