網(wǎng)站首頁 編程語言 正文
介紹
這是一個(gè)簡單的時(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()信號是否發(fā)出 //timeout()表示:This signal is emitted when the timer times out. //指計(jì)時(shí)器發(fā)出信號,即下面的延時(shí)器發(fā)出信號 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è)信號發(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)大的畫圖工具 painter.setRenderHint(QPainter::Antialiasing);// 用于反鋸齒 //針對所有的組件,都反鋸齒//表示設(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畫板的x和y表示什么,x表示橫線嗎,y表示縱線嗎?對的 //說明橫坐標(biāo)的范圍是-100到100 // 縱坐標(biāo)的范圍是-100到100 //時(shí)針: painter.setPen(Qt::NoPen);//一般用于描邊,Qt::NoPen表示畫筆沒有邊界 painter.setBrush(hourColor);//一般用于填充 //先將原先的painter存儲(chǔ)起來,對目前的painter操作,目前的操作不對原本的產(chǎn)生影響,即原本不旋轉(zhuǎn) painter.save();//首先將原先畫筆類似于入棧,對另一個(gè)畫筆操作 painter.rotate(30.0 * ((time.hour() + time.minute() / 60.0)));//表示旋轉(zhuǎn),若缺少painter.save(),會(huì)對整個(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);//畫橫線,表示時(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(); }
編譯打包
編譯
一般編譯過程采用的是debug版本,但是給其他用戶使用最好是release版本,因此打包前需要切換到release版本重新編譯一遍。
這樣在項(xiàng)目文件夾中會(huì)有兩種版本的exe執(zhí)行程序。
打包
生成release版本的exe后,進(jìn)入文件夾中,將release文件夾中的clock.exe復(fù)制到單獨(dú)的文件夾中 ,我復(fù)制到myClock文件夾中。
在開始菜單中,選擇下圖紅色的cmd。
進(jìn)入到myClock文件夾中,輸入 windeployqt clock.exe?
打包完成后,在myClock文件夾中就可以看到各種.dll鏈接庫文件,這是exe文件依賴的庫文件,此時(shí)雙擊clock.exe就可以動(dòng)態(tài)顯示時(shí)鐘了。
將該文件夾打包,就可以部署到其他的Windows系統(tǒng)上。
原文鏈接:https://blog.csdn.net/weixin_44753371/article/details/122583294
相關(guān)推薦
- 2023-02-15 Android音視頻開發(fā)只硬件解碼組件MediaCodec講解_Android
- 2022-03-30 C#算法之整數(shù)反轉(zhuǎn)_C#教程
- 2022-02-26 Hutool cn.hutool.core.bean.BeanException: Set valu
- 2022-09-26 Dockerfile部署帶有ssh的Ubuntu
- 2023-06-18 Python實(shí)現(xiàn)兩種稀疏矩陣的最小二乘法_python
- 2022-08-11 python實(shí)現(xiàn)對excel中需要的數(shù)據(jù)的單元格填充顏色_python
- 2021-12-10 linux中的軟連接和硬連接詳解_Linux
- 2023-05-06 docker?search命令的具體使用_docker
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細(xì)win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運(yùn)行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲(chǔ)小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運(yùn)算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認(rèn)證信息的處理
- Spring Security之認(rèn)證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯(cuò)誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實(shí)現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡單動(dòng)態(tài)字符串(SD
- arthas操作spring被代理目標(biāo)對象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊(duì)列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠(yuǎn)程分支