QT5實(shí)現(xiàn)電子時(shí)鐘
本文實(shí)例為大家分享了QT5實(shí)現(xiàn)電子時(shí)鐘的具體代碼,供大家參考,具體內(nèi)容如下
一、電子時(shí)鐘的顯示
效果如下:

電子時(shí)鐘顯示
二、新建工程
Widgets Application項(xiàng)目名位clock,基礎(chǔ)類位QDialog,取消創(chuàng)建UI界面的勾選框,項(xiàng)目名右擊添加新文件
在彈出的對(duì)話框中選擇“C++ Class”,Base class基礎(chǔ)類名“QLCDNumber”,class name命名為digiclock,點(diǎn)擊完成。
三、編輯digiclock.h文件
#ifndef DIGICLOCK_H
#define DIGICLOCK_H
#include <QLCDNumber>
?
class DigiClock : public QLCDNumber
{
public:
? ? DigiClock(QWidget *parent=0);
? ? void mousePressEvent(QMouseEvent *);
? ? void mouseMoveEvent(QMouseEvent *);
private slots:
? ? void showTime();//顯示槽函數(shù)
?
private:
? ? QPoint dragPosition;//相對(duì)位置偏移
? ? bool showColon;//是否顯示“:”
? ? QTimer *mtimer;
};
?
#endif // DIGICLOCK_H四、編輯digiclock.cpp文件
#include "digiclock.h"
#include <QTime>
#include <QTimer>
#include <QMouseEvent>
#include <QDebug>
?
DigiClock::DigiClock(QWidget *parent):QLCDNumber(parent)
{
? ? QPalette p = palette();//
? ? p.setColor(QPalette::Window,Qt::blue);
? ? setPalette(p);//設(shè)置窗體顏色
? ? setWindowFlags(Qt::FramelessWindowHint);//窗體設(shè)置位無(wú)邊框
? ? setWindowOpacity(0.5);//設(shè)置透明度
? ? mtimer = new QTimer(this);//new 定時(shí)器對(duì)象
? ? //下列方法1不可以定時(shí)
? ? //connect(mtimer,SIGNAL(timeout()),this,SLOT(showTime()));
? ? //下列方法2可以實(shí)現(xiàn)定時(shí)
? ? connect(mtimer,&QTimer::timeout,[=](){showTime();}); ??
? ? if(mtimer->isActive()==false)//定時(shí)器檢查激活狀態(tài)
? ? {
? ? mtimer->start(1000);//啟動(dòng)
? ? }
? ? showTime();//槽函數(shù)
? ? resize(300,60);
? ? showColon=true;
}
void DigiClock::showTime()
{
? ? QTime time1 = QTime::currentTime();//獲取當(dāng)前時(shí)間
? ? QString text = time1.toString("hh:mm:ss");
? ? this->setDigitCount(8);//設(shè)置顯示長(zhǎng)度
?
? ? if(showColon)
? ? {
? ? ? ? text[2]=':';
? ? ? ? text[5]=':';
? ? ? ? showColon=false;
? ? }else
? ? {
? ? ? ? text[2]=' ';
? ? ? ? text[5]=' ';
? ? ? ? showColon=true;
? ? }
? ? ?//qDebug()<<text;
? ? ?display(text);
}
void DigiClock::mousePressEvent(QMouseEvent *event)
{
? ? if(event->button()==Qt::LeftButton)
? ? {
? ? ? ? //獲取移動(dòng)參考點(diǎn)
? ? ? ? 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);//拖拽移動(dòng)
? ? ? ? event->accept();
? ? }
}五、編輯主函數(shù)
#include "dialog.h"
?
#include <QApplication>
#include "digiclock.h"
?
int main(int argc, char *argv[])
{
? ? QApplication a(argc, argv);
? ? DigiClock w;
? ? w.show();
? ? return a.exec();
}六、總結(jié)
調(diào)試方法1時(shí),connect(mtimer,SIGNAL(timeout()),this,SLOT(showTime()));不能實(shí)現(xiàn)定時(shí)的效果,糾結(jié)了好一陣,還是沒(méi)發(fā)現(xiàn)問(wèn)題,可能時(shí)QT書寫形式更新了?
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
關(guān)于C語(yǔ)言一維數(shù)組算法問(wèn)題詳解
數(shù)組是以順序格式排列的均勻數(shù)據(jù)的集合,在C語(yǔ)言中學(xué)習(xí)數(shù)組的概念非常重要,因?yàn)樗腔镜臄?shù)據(jù)結(jié)構(gòu),這篇文章主要給大家介紹了關(guān)于C語(yǔ)言一維數(shù)組算法問(wèn)題的相關(guān)資料,需要的朋友可以參考下2021-11-11
C語(yǔ)言中enum關(guān)鍵字的實(shí)現(xiàn)示例
這篇文章主要介紹了C語(yǔ)言中enum關(guān)鍵字的實(shí)現(xiàn)示例,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2021-03-03
通過(guò)實(shí)例詳解C語(yǔ)言函數(shù)返回值
函數(shù)的返回值是指函數(shù)被調(diào)用之后,執(zhí)行函數(shù)體中的程序段所取得的并返回給主調(diào)函數(shù)的值,下面這篇文章主要給大家介紹了關(guān)于C語(yǔ)言函數(shù)返回值的相關(guān)資料,需要的朋友可以參考下2022-01-01
C++面試八股文之STL標(biāo)準(zhǔn)模板庫(kù)使用詳解
這篇文章主要為大家介紹了C++面試八股文之STL標(biāo)準(zhǔn)模板庫(kù)使用詳解,<BR>有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-06-06
C語(yǔ)言中改變目錄的相關(guān)操作函數(shù)詳解
這篇文章主要介紹了C語(yǔ)言中改變目錄的相關(guān)操作函數(shù)詳解,分別是fchdir()函數(shù)和rewinddir()函數(shù)的使用方法,需要的朋友可以參考下2015-09-09
C語(yǔ)言用數(shù)組實(shí)現(xiàn)反彈球消磚塊
這篇文章主要為大家詳細(xì)介紹了C語(yǔ)言用數(shù)組實(shí)現(xiàn)反彈球消磚塊,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-05-05
visual?studio?2022一個(gè)不易發(fā)現(xiàn)的問(wèn)題
本文主要介紹了visual?studio?2022一個(gè)不易發(fā)現(xiàn)的問(wèn)題,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2022-07-07
C++中unique_lock和lock_guard區(qū)別小結(jié)
本文主要介紹了C++中unique_lock和lock_guard區(qū)別,std::unique_lock?和?std::lock_guard屬于標(biāo)準(zhǔn)庫(kù)mute中的互斥鎖管理工具,用于簡(jiǎn)化互斥鎖的使用并確保線程安全,具有一定的參考價(jià)值,感興趣的可以了解一下2025-04-04
C語(yǔ)言實(shí)現(xiàn)簡(jiǎn)易通訊錄完整流程
這篇文章主要為大家介紹了C語(yǔ)言實(shí)現(xiàn)簡(jiǎn)易通訊錄的完整流程,每個(gè)環(huán)節(jié)都有完整代碼,有需要的朋友可以借鑒參考下,希望能夠有所幫助2022-02-02

