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

實現(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);
//實例一個QTimer的類
connect(timer, SIGNAL(timeout()), this, SLOT(update()));
//監(jiān)控timeout()信號是否發(fā)出
//timeout()表示:This signal is emitted when the timer times out.
//指計時器發(fā)出信號,即下面的延時器發(fā)出信號
timer->start(1000);//設(shè)置1s的延時
/*
*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è)置一個信號發(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)
};//用于繪制時針的三角形
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);
//為每一個圖形繪制顏色及透明度
int side = qMin(width(), height());
//我認為這一句的作用在于找到最小標出,用于坐標系的繪制
QTime time = QTime::currentTime();
qDebug()<<time<<'\n';//用于檢驗現(xiàn)在的時間
QPainter painter(this);//Qt強大的畫圖工具
painter.setRenderHint(QPainter::Antialiasing);// 用于反鋸齒
//針對所有的組件,都反鋸齒//表示設(shè)置渲染提示
painter.translate(width() / 2, height() / 2);//將原點放在中心
painter.scale(side / 200.0, side / 200.0);//Scales the coordinate system by (sx, sy).標尺坐標系
//Qt畫板的x和y表示什么,x表示橫線嗎,y表示縱線嗎?對的
//說明橫坐標的范圍是-100到100
// 縱坐標的范圍是-100到100
//時針:
painter.setPen(Qt::NoPen);//一般用于描邊,Qt::NoPen表示畫筆沒有邊界
painter.setBrush(hourColor);//一般用于填充
//先將原先的painter存儲起來,對目前的painter操作,目前的操作不對原本的產(chǎn)生影響,即原本不旋轉(zhuǎn)
painter.save();//首先將原先畫筆類似于入棧,對另一個畫筆操作
painter.rotate(30.0 * ((time.hour() + time.minute() / 60.0)));//表示旋轉(zhuǎn),若缺少painter.save(),會對整個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);//畫橫線,表示時間示數(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);
}
//時針:
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();
}
編譯打包
編譯
一般編譯過程采用的是debug版本,但是給其他用戶使用最好是release版本,因此打包前需要切換到release版本重新編譯一遍。

這樣在項目文件夾中會有兩種版本的exe執(zhí)行程序。
打包
生成release版本的exe后,進入文件夾中,將release文件夾中的clock.exe復制到單獨的文件夾中 ,我復制到myClock文件夾中。
在開始菜單中,選擇下圖紅色的cmd。

進入到myClock文件夾中,輸入 windeployqt clock.exe

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

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

到此這篇關(guān)于基于QT5實現(xiàn)一個時鐘桌面的文章就介紹到這了,更多相關(guān)QT5時鐘桌面內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
C++ new與malloc和delete及free動態(tài)內(nèi)存管理及區(qū)別介紹
這篇文章主要介紹了深入理解C++中的new/delete和malloc/free動態(tài)內(nèi)存管理,本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2022-12-12
二維指針動態(tài)分配內(nèi)存連續(xù)問題深入分析
當我們定義一個二維指針時,如果需要存儲相應(yīng)的數(shù)據(jù),就需要我們動態(tài)的分配內(nèi)存,這時,有一點是需要注意的,分配內(nèi)存的方法不同,內(nèi)存的連續(xù)性也是不相同的2013-07-07

