基于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語(yǔ)言實(shí)現(xiàn)五子棋對(duì)戰(zhàn)系統(tǒng)
這篇文章主要為大家詳細(xì)介紹了C語(yǔ)言實(shí)現(xiàn)五子棋對(duì)戰(zhàn)系統(tǒng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-05-05
C語(yǔ)言實(shí)現(xiàn)掃雷游戲簡(jiǎn)易版
這篇文章主要為大家詳細(xì)介紹了C語(yǔ)言實(shí)現(xiàn)掃雷游戲簡(jiǎn)易版,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-11-11
深入剖析Android中init進(jìn)程實(shí)現(xiàn)的C語(yǔ)言源碼
這篇文章主要介紹了Android中init進(jìn)程實(shí)現(xiàn)的C語(yǔ)言源碼,init屬性服務(wù)在安卓中屬于系統(tǒng)的底層Linux服務(wù),需要的朋友可以參考下2015-07-07
關(guān)于AVLTree(C++實(shí)現(xiàn))沒(méi)有統(tǒng)一旋轉(zhuǎn)操作的問(wèn)題
這篇文章主要介紹了關(guān)于AVLTree(C++實(shí)現(xiàn))沒(méi)有統(tǒng)一旋轉(zhuǎn)操作的問(wèn)題,本文給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-02-02
QT使用QComBox和QLineEdit實(shí)現(xiàn)模糊查詢功能
模糊查詢是指根據(jù)用戶輸入的文本,在下拉框的選項(xiàng)中進(jìn)行模糊匹配,并動(dòng)態(tài)地顯示匹配的選項(xiàng),本文將使用QComBox和QLineEdit實(shí)現(xiàn)模糊查詢功能,需要的可以參考下2023-11-11
C++ OpenCV實(shí)戰(zhàn)之文檔照片轉(zhuǎn)換成掃描文件
這篇文章主要為大家介紹一個(gè)C++?OpenCV的實(shí)戰(zhàn)——文檔照片轉(zhuǎn)換成掃描文件,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2022-09-09
c++ 判斷是64位還是32位系統(tǒng)的實(shí)例
這篇文章主要介紹了c++ 判斷是64位還是32位系統(tǒng)的實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-12-12

