QTimer與QTime實現(xiàn)電子時鐘
本文實例為大家分享了QTimer與QTime實現(xiàn)電子時鐘的具體代碼,供大家參考,具體內(nèi)容如下
使用QLCDNumber控件進(jìn)行顯示
QLCDNumber控件默認(rèn)只顯示5個字符,可以使用setDigitCount(int size)進(jìn)行設(shè)置顯示個數(shù)
使用Display(QString str) 設(shè)置顯示內(nèi)容
該函數(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; // 鼠標(biāo)坐標(biāo)跟窗口左上角坐標(biāo)的偏移值
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); // 設(shè)置精度為較高精度,差距在毫秒內(nèi)
timer->start(1000);
connect(timer, SIGNAL(timeout()), this, SLOT(updateTime()),Qt::QueuedConnection);
setWindowFlag(Qt::FramelessWindowHint); //沒有面板邊框標(biāo)題欄的窗體
setWindowOpacity(0.5); //設(shè)置窗口的透明度
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);
}
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
C++獲取特定進(jìn)程CPU使用率的實現(xiàn)代碼
寫一個小程序在后臺記錄每個進(jìn)程的CPU使用情況,揪出鎖屏后占用CPU的進(jìn)程,于是自己寫了一個C++類CPUusage,方便地監(jiān)視不同進(jìn)程的CPU占用情況。本人編程還只是個新手,如有問題請多多指教2019-04-04
C語言實現(xiàn)簡單學(xué)生學(xué)籍管理系統(tǒng)
這篇文章主要為大家詳細(xì)介紹了C語言實現(xiàn)簡單學(xué)生學(xué)籍管理系統(tǒng),具有一定的參考價值,感興趣的小伙伴們可以參考一下2018-01-01

