Qt實(shí)現(xiàn)電子時(shí)鐘
本文實(shí)例為大家分享了Qt實(shí)現(xiàn)電子時(shí)鐘的具體代碼,供大家參考,具體內(nèi)容如下
進(jìn)一步認(rèn)識(shí)Qt中的屬性,我們現(xiàn)在再做一個(gè)小練習(xí),去實(shí)現(xiàn)一個(gè)簡(jiǎn)易版電子時(shí)鐘的效果。
效果展示:
新建項(xiàng)目:
我們創(chuàng)建了lcdclock2類(lèi),其實(shí)是沒(méi)有用到的,實(shí)際上是添加新一個(gè)C++文件來(lái)實(shí)現(xiàn)電子鐘。
添加C++文件:
代碼:
clock.h
#ifndef CLOCK_H #define CLOCK_H ? #include <QDialog> ? class clock : public QDialog { ? ? Q_OBJECT ? public: ? ? clock(QWidget *parent = 0); ? ? ~clock(); }; ? #endif // CLOCK_H
這個(gè)clock文件沒(méi)有啥用處,其實(shí)就是為了創(chuàng)建對(duì)話框存在的。
digitalclock.h
#ifndef DIGITALCLOCK_H #define DIGITALCLOCK_H #include<QLCDNumber> ? class digitalclock : public QLCDNumber { ? ? Q_OBJECT public: ? ? digitalclock(QWidget* parent=0); protected: ? ? void mousePressEvent(QMouseEvent* event); ? ? void mouseMoveEvent(QMouseEvent* event); private slots: ? ? void showTime(); private: ? ? QPoint m_dragPosition; ? ? bool m_showColon; }; ? #endif // DIGITALCLOCK_H
添加的digitalclock頭文件添加頭文件QLCDNumber,在添加元對(duì)象系統(tǒng),這個(gè)是必要的。接著需要重寫(xiě)鼠標(biāo)點(diǎn)擊事件mousePressEvent還有mouseMoveEvent鼠標(biāo)移動(dòng)事件。再寫(xiě)一個(gè)槽函數(shù)用來(lái)顯示時(shí)間,就是將時(shí)間打印到對(duì)話框里面。這里有兩個(gè)私有變量一個(gè)是m_dragPosition用來(lái)保存鼠標(biāo)點(diǎn)擊點(diǎn)到窗口左上方的位置距離。另外有一個(gè)變量是bool類(lèi)型 m_showColon用來(lái)保存冒號(hào)出現(xiàn)的狀態(tài),達(dá)到頻閃狀態(tài)。
clock.cpp
#include "clock.h" ? clock::clock(QWidget *parent) ? ? : QDialog(parent) { } ? clock::~clock() { ? }
digitalclock.cpp
#include "digitalclock.h" #include<QMouseEvent> #include<QDebug> #include<QTime> #include<QTimer> digitalclock::digitalclock(QWidget* parent):QLCDNumber(parent) { ? ? ? //設(shè)置藍(lán)色背景 ? ? QPalette p=palette();//palette函數(shù)返回一個(gè)調(diào)色板 ? ? p.setColor(QPalette::Window,Qt::blue); ? ? setPalette(p);//將獲取的調(diào)色板設(shè)置為窗體的顏色 ? ? ? //設(shè)置邊框格式 ? ? setWindowFlags(Qt::FramelessWindowHint); ? ? ? //設(shè)置窗體透明度 ? ? setWindowOpacity(0.5); ? ? ? QTimer* timer=new QTimer(this); ? ? connect(timer,&QTimer::timeout,this,&digitalclock::showTime); ? ? timer->start(1000);//設(shè)置計(jì)時(shí)器周期 ? ? ? showTime(); ? ? resize(200,100); ? ? m_showColon=true; } ? void digitalclock::mousePressEvent(QMouseEvent *event) { ? ? if(event->button()==Qt::LeftButton){ ? ? ? ? m_dragPosition=event->globalPos()-frameGeometry().topLeft(); ? ? ? ? qDebug()<<"全局坐標(biāo):"<<event->globalPos(); ? ? ? ? qDebug()<<"左上角坐標(biāo)"<<frameGeometry().topLeft(); ? ? ? ? event->accept(); ? ? }else{ ? ? ? ? close();//關(guān)閉窗口 ? ? ? } ? } ? void digitalclock::mouseMoveEvent(QMouseEvent *event) { ? ? if(event->buttons()==Qt::LeftButton){ ? ? ? ? ? move(event->globalPos()-m_dragPosition); ? ? ? ? //移動(dòng)電子時(shí)鐘,move函數(shù)實(shí)際是根據(jù)窗口左上角為焦點(diǎn)來(lái)移動(dòng),如果不減去鼠標(biāo)和左上角的偏移值,窗口會(huì)自動(dòng)偏移 ? ? ? ? ? event->accept(); ? ? } ? } ? void digitalclock::showTime() { ? ? QTime time=QTime::currentTime();//定義一個(gè)時(shí)間對(duì)象,獲取當(dāng)前的時(shí)間 ? ? QString strTime=time.toString("hh:mm");//將時(shí)間轉(zhuǎn)為字符串,并且指定格式 ? ? if(m_showColon){ ? ? ? ? strTime[2]=':'; ? ? }else{ ? ? ? ? strTime[2]=' '; ? ? } ? ? display(strTime); ? ? m_showColon=!m_showColon;//將冒號(hào)的顯示狀態(tài)設(shè)置為與之前想反,這樣就達(dá)到頻閃的樣子 ? }
構(gòu)造函數(shù):
首先定義一個(gè)調(diào)色板對(duì)象,利用palette()函數(shù)返回一個(gè)調(diào)色板。在利用調(diào)色板的函數(shù)setColror(顏色類(lèi)型,顏色);設(shè)置窗體風(fēng)格函數(shù)用到setWIndowFlags();設(shè)置窗體透明度函數(shù)是setWIndowOpacity函數(shù)。緊接著再定義一個(gè)計(jì)時(shí)器QTimer.再將計(jì)時(shí)器與showTIme槽函數(shù)聯(lián)系起來(lái),每當(dāng)計(jì)時(shí)器的時(shí)間到的時(shí)候就調(diào)用槽函數(shù),之后再利用resize函數(shù)將窗口的大小重新設(shè)置。首先將冒號(hào)顯示狀態(tài)設(shè)置為真。
鼠標(biāo)點(diǎn)擊函數(shù)mousePreessEvent函數(shù):
首先判斷鼠標(biāo)是否為左鍵點(diǎn)擊,如果是那就將鼠標(biāo)點(diǎn)擊的點(diǎn)和窗口左上角點(diǎn)的距離計(jì)算出來(lái),保存在m_dragposition。每次到函數(shù)結(jié)尾的時(shí)候一定要帶上event.accept接受函數(shù)修改。
鼠標(biāo)移動(dòng)事件mouseMoveEvent函數(shù):
首先判斷鼠標(biāo)左鍵是否被點(diǎn)擊,因?yàn)橥ǔ5那闆r下,鼠標(biāo)的移動(dòng)是按左鍵拖動(dòng)的,move函數(shù)實(shí)際是以窗口的左上方為焦點(diǎn)的,如果我們單純傳鼠標(biāo)的全局坐標(biāo),那這樣窗口的左上方會(huì)跳動(dòng)到鼠標(biāo)的點(diǎn)擊點(diǎn)。這樣就不好看,所以在傳進(jìn)坐標(biāo)的時(shí)候,就是傳鼠標(biāo)點(diǎn)擊的點(diǎn)減去窗口左上方相對(duì)于鼠標(biāo)按下的偏移位置。
時(shí)間顯示函數(shù)showTIme函數(shù):
時(shí)間顯示函數(shù)就是定義一個(gè)時(shí)間對(duì)象,利用QTime::Current函數(shù)獲取當(dāng)前的時(shí)間。在將時(shí)間轉(zhuǎn)成指定的格式字符串,緊接著判斷冒號(hào)的顯示狀態(tài),將冒號(hào)根據(jù)狀態(tài)設(shè)置有無(wú)。顯示時(shí)間真正的函數(shù)是display函數(shù),將時(shí)間字符串打印到窗口內(nèi)。之后再將冒號(hào)狀態(tài)取反,達(dá)到每個(gè)一秒頻閃。
mian.cpp
#include "clock.h" #include <QApplication> #include"digitalclock.h" int main(int argc, char *argv[]) { ? ? QApplication a(argc, argv); ? ? digitalclock w; ? ? w.show(); ? ? ? return a.exec(); }
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
DSP中浮點(diǎn)轉(zhuǎn)定點(diǎn)運(yùn)算--浮點(diǎn)與定點(diǎn)概述
本文主要介紹DSP中浮點(diǎn)與定點(diǎn)概述,很值得學(xué)習(xí)一下,需要的朋友可以參考一下。2016-06-06C語(yǔ)言實(shí)現(xiàn)電子時(shí)鐘程序
這篇文章主要為大家詳細(xì)介紹了C語(yǔ)言實(shí)現(xiàn)電子時(shí)鐘程序,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-11-11C++中的string庫(kù)函數(shù)常見(jiàn)函數(shù)的作用和使用方法
這篇文章主要介紹了C++中的string庫(kù)函數(shù)常見(jiàn)函數(shù)的作用和使用方法,庫(kù)函數(shù)的靈活應(yīng)用是程序員的一大重要技能,本文通過(guò)實(shí)例實(shí)例代碼給大家講解的非常詳細(xì),需要的朋友可以參考下2022-04-04C++ 模擬實(shí)現(xiàn)list(迭代器)實(shí)現(xiàn)代碼
這篇文章主要介紹了C++ 模擬實(shí)現(xiàn)list(迭代器)實(shí)現(xiàn)代碼的相關(guān)資料,需要的朋友可以參考下2017-05-05