基于QT5實(shí)現(xiàn)一個(gè)時(shí)鐘桌面
介紹
這是一個(gè)簡(jiǎn)單的時(shí)鐘運(yùn)行界面,項(xiàng)目的結(jié)構(gòu)如圖所示,主要包含一個(gè)頭文件:** analogclock.h **,兩個(gè)源文件: ** analogclock.cpp main.cpp **.
看完本教程,你就可以知悉在Windows系統(tǒng)上如何實(shí)現(xiàn)一個(gè)界面程序并部署在Windows系統(tǒng)上。

實(shí)現(xiàn)代碼
clock.pro
QT += core gui
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
CONFIG += c++11
# You can make your code fail to compile if it uses deprecated APIs.
# In order to do so, uncomment the following line.
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0
SOURCES += \
main.cpp \
analogclock.cpp
HEADERS += \
analogclock.h
# Default rules for deployment.
qnx: target.path = /tmp/$${TARGET}/bin
else: unix:!android: target.path = /opt/$${TARGET}/bin
!isEmpty(target.path): INSTALLS += target
analogclock.h
#ifndef ANALOGCLOCK_H
#define ANALOGCLOCK_H
#include <QWidget>
class AnalogClock : public QWidget
{
Q_OBJECT
public:
AnalogClock(QWidget *parent=0);
protected:
void paintEvent(QPaintEvent *event) override;
};
#endif // WIDGET_H
analogclock.cpp
#include <QtWidgets>
#include "analogclock.h"
AnalogClock::AnalogClock(QWidget *parent)
: QWidget(parent)
{
QTimer *timer = new QTimer(this);
//實(shí)例一個(gè)QTimer的類
connect(timer, SIGNAL(timeout()), this, SLOT(update()));
//監(jiān)控timeout()信號(hào)是否發(fā)出
//timeout()表示:This signal is emitted when the timer times out.
//指計(jì)時(shí)器發(fā)出信號(hào),即下面的延時(shí)器發(fā)出信號(hào)
timer->start(1000);//設(shè)置1s的延時(shí)
/*
*for a function
* void QTimer::start(int msec)
* Starts or restarts the timer with a timeout interval of msec milliseconds.
* If the timer is already running, it will be stopped and restarted.
* If singleShot is true, the timer will be activated only once.
* 單位是毫秒,表示每一秒設(shè)置一個(gè)信號(hào)發(fā)出
*/
setWindowTitle(tr("Analog Clock"));
//void setWindowTitle(const QString &)
resize(200, 200);
//初始值大小
}
void AnalogClock::paintEvent(QPaintEvent *)
{
/*
*
* repaint() or update() was invoked,
* the widget was obscured and has now been uncovered, or
* many other reasons.
*
*
*/
static const QPoint hourHand[3] = {
QPoint(7, 8),
QPoint(-7, 8),
QPoint(0, -40)
};//用于繪制時(shí)針的三角形
static const QPoint minuteHand[3] = {
QPoint(7, 8),
QPoint(-7, 8),
QPoint(0, -60)
};//用于繪制分針的三角形
static const QPoint secondHand[3]={
QPoint(7,8),
QPoint(-7,8),
QPoint(0,-90)
};//用于繪制秒針的三角形
QColor hourColor(127, 0, 127);
QColor minuteColor(0, 127, 127, 191);
//QColor::QColor(int r, int g, int b, int a = 255)a表示透明度
QColor secondColor(220,20,60,100);
//為每一個(gè)圖形繪制顏色及透明度
int side = qMin(width(), height());
//我認(rèn)為這一句的作用在于找到最小標(biāo)出,用于坐標(biāo)系的繪制
QTime time = QTime::currentTime();
qDebug()<<time<<'\n';//用于檢驗(yàn)現(xiàn)在的時(shí)間
QPainter painter(this);//Qt強(qiáng)大的畫(huà)圖工具
painter.setRenderHint(QPainter::Antialiasing);// 用于反鋸齒
//針對(duì)所有的組件,都反鋸齒//表示設(shè)置渲染提示
painter.translate(width() / 2, height() / 2);//將原點(diǎn)放在中心
painter.scale(side / 200.0, side / 200.0);//Scales the coordinate system by (sx, sy).標(biāo)尺坐標(biāo)系
//Qt畫(huà)板的x和y表示什么,x表示橫線嗎,y表示縱線嗎?對(duì)的
//說(shuō)明橫坐標(biāo)的范圍是-100到100
// 縱坐標(biāo)的范圍是-100到100
//時(shí)針:
painter.setPen(Qt::NoPen);//一般用于描邊,Qt::NoPen表示畫(huà)筆沒(méi)有邊界
painter.setBrush(hourColor);//一般用于填充
//先將原先的painter存儲(chǔ)起來(lái),對(duì)目前的painter操作,目前的操作不對(duì)原本的產(chǎn)生影響,即原本不旋轉(zhuǎn)
painter.save();//首先將原先畫(huà)筆類似于入棧,對(duì)另一個(gè)畫(huà)筆操作
painter.rotate(30.0 * ((time.hour() + time.minute() / 60.0)));//表示旋轉(zhuǎn),若缺少painter.save(),會(huì)對(duì)整個(gè)painter類旋轉(zhuǎn)
painter.drawConvexPolygon(hourHand, 3);//繪制多邊形
painter.restore();//與painter.save()配套使用
painter.setPen(hourColor);
for (int i = 0; i < 12; ++i) {
painter.drawLine(88, 0, 96, 0);
painter.rotate(30.0);//畫(huà)橫線,表示時(shí)間示數(shù)的標(biāo)尺
}//分針和秒針同時(shí)針
//分針:
painter.setPen(Qt::NoPen);
painter.setBrush(minuteColor);
painter.save();
painter.rotate(6.0 * (time.minute() + time.second() / 60.0));
painter.drawConvexPolygon(minuteHand, 3);
painter.restore();
painter.setPen(minuteColor);
for (int j = 0; j < 60; ++j) {
if ((j % 5) != 0)
painter.drawLine(92, 0, 96, 0);
painter.rotate(6.0);
}
//時(shí)針:
painter.setPen(Qt::NoPen);
painter.setBrush(secondColor);
painter.save();
painter.rotate(6*time.second());
painter.drawConvexPolygon(secondHand,3);
painter.restore();
}
main.cpp
#include "analogclock.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
AnalogClock w;
w.show();
return a.exec();
}
編譯打包
編譯
一般編譯過(guò)程采用的是debug版本,但是給其他用戶使用最好是release版本,因此打包前需要切換到release版本重新編譯一遍。

這樣在項(xiàng)目文件夾中會(huì)有兩種版本的exe執(zhí)行程序。
打包
生成release版本的exe后,進(jìn)入文件夾中,將release文件夾中的clock.exe復(fù)制到單獨(dú)的文件夾中 ,我復(fù)制到myClock文件夾中。
在開(kāi)始菜單中,選擇下圖紅色的cmd。

進(jìn)入到myClock文件夾中,輸入 windeployqt clock.exe

打包完成后,在myClock文件夾中就可以看到各種.dll鏈接庫(kù)文件,這是exe文件依賴的庫(kù)文件,此時(shí)雙擊clock.exe就可以動(dòng)態(tài)顯示時(shí)鐘了。

將該文件夾打包,就可以部署到其他的Windows系統(tǒng)上。

到此這篇關(guān)于基于QT5實(shí)現(xiàn)一個(gè)時(shí)鐘桌面的文章就介紹到這了,更多相關(guān)QT5時(shí)鐘桌面內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
C++ new與malloc和delete及free動(dòng)態(tài)內(nèi)存管理及區(qū)別介紹
這篇文章主要介紹了深入理解C++中的new/delete和malloc/free動(dòng)態(tài)內(nèi)存管理,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-12-12
C語(yǔ)言實(shí)現(xiàn)餐飲管理系統(tǒng)
這篇文章主要為大家詳細(xì)介紹了C語(yǔ)言實(shí)現(xiàn)餐飲管理系統(tǒng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-03-03
二維指針動(dòng)態(tài)分配內(nèi)存連續(xù)問(wèn)題深入分析
當(dāng)我們定義一個(gè)二維指針時(shí),如果需要存儲(chǔ)相應(yīng)的數(shù)據(jù),就需要我們動(dòng)態(tài)的分配內(nèi)存,這時(shí),有一點(diǎn)是需要注意的,分配內(nèi)存的方法不同,內(nèi)存的連續(xù)性也是不相同的2013-07-07
C++中棧結(jié)構(gòu)建立與操作詳細(xì)解析
我們可以把棧理解成一個(gè)大倉(cāng)庫(kù),放在倉(cāng)庫(kù)門(mén)口(棧頂)的貨物會(huì)優(yōu)先被取出,然后再取出里面的貨物。而從數(shù)據(jù)的邏輯結(jié)構(gòu)來(lái)看,棧結(jié)構(gòu)起始就是一種線性結(jié)構(gòu)2013-10-10

