Qt編寫自定義控制元件19-圖片背景時鐘
前言
圖片背景時鐘控制元件,是全套控制元件(目前共145個)中唯一的幾個貼圖的控制元件,這個背景要是不貼圖,會畫到猝死,必須用美工做好的圖貼圖作為背景,此控制元件以前學C#的時候寫過,後面在寫Qt控制元件的過程中把他移植過來了,其實畫法完全一模一樣,我能說連程式碼我都是直接複製貼上過來改改的嗎?所以有過多年程式設計經驗的程式設計師們都知道,程式設計都是一通百通的,只要掌握好了一門,或者精通了一門,其他都是水到渠成的事情,基本上學習個把星期都能直接擼的那種,配合F1幫助文件和官方手冊,直接手擼起來(各位別多想,是指擼程式碼)。 貼圖的控制元件都很簡單,直接drawimage完事,本控制元件除了支援多種背景風格樣式以外,還特意增加了指標走動風格樣式,直接滑鼠右鍵切換風格等。
實現的功能
- 1:支援滑鼠右鍵切換風格
- 2:支援設定四種背景風格樣式
- 3:支援四種秒針走動風格樣式
- 4:增加設定時間介面
效果圖
標頭檔案程式碼
#ifndef IMAGECLOCK_H #define IMAGECLOCK_H /** * 圖片時鐘控制元件 作者:feiyangqingyun(QQ:517216493) 2016-11-4 * 1:支援滑鼠右鍵切換風格 * 2:支援設定四種背景風格樣式 * 3:支援四種秒針走動風格樣式 * 4:增加設定時間介面 */ #include <QWidget> #ifdef quc #if (QT_VERSION < QT_VERSION_CHECK(5,7,0)) #include <QtDesigner/QDesignerExportWidget> #else #include <QtUiPlugin/QDesignerExportWidget> #endif class QDESIGNER_WIDGET_EXPORT ImageClock : public QWidget #else class ImageClock : public QWidget #endif { Q_OBJECT Q_ENUMS(ClockStyle) Q_ENUMS(SecondStyle) Q_PROPERTY(ClockStyle clockStyle READ getClockStyle WRITE setClockStyle) Q_PROPERTY(SecondStyle secondStyle READ getSecondStyle WRITE setSecondStyle) public: enum ClockStyle { ClockStyle_Trad = 0, //黑色風格 ClockStyle_System = 1, //銀色風格 ClockStyle_Modern = 2, //紅色風格 ClockStyle_Flower = 3 //花瓣風格 }; enum SecondStyle { SecondStyle_Normal = 0, //普通效果 SecondStyle_Spring = 1, //彈簧效果 SecondStyle_Continue = 2, //連續效果 SecondStyle_Hide = 3 //隱藏效果 }; explicit ImageClock(QWidget *parent = 0); ~ImageClock(); protected: void paintEvent(QPaintEvent *); void drawBg(QPainter *painter); void drawHour(QPainter *painter); void drawMin(QPainter *painter); void drawSec(QPainter *painter); void drawDot(QPainter *painter); private: ClockStyle clockStyle; //背景樣式 SecondStyle secondStyle; //秒針走動樣式 QImage clockBg; //主背景 QImage clockHour; //時鐘背景 QImage clockMin; //分鐘背景 QImage clockSec; //秒鐘背景 QImage clockDot; //中間點背景 QImage clockHighlights; //高亮背景 QStringList imageNames; //圖片名稱集合 QTimer *timer; //定時器計算時間 int hour, min, sec, msec; //時分秒毫秒 QTimer *timerSpring; //定時器顯示彈簧效果 double angleSpring; //彈簧角度 QAction *action_secondstyle;//秒針樣式右鍵選單 private Q_SLOTS: void doAction(); void updateTime(); void updateSpring(); public: ClockStyle getClockStyle() const; SecondStyle getSecondStyle() const; QSize sizeHint() const; QSize minimumSizeHint() const; public Q_SLOTS: //設定圖片背景時鐘樣式 void setClockStyle(const ClockStyle &clockStyle); //設定秒針走動樣式 void setSecondStyle(const SecondStyle &secondStyle); //設定系統時間 void setSystemDateTime(const QString &year, const QString &month, const QString &day, const QString &hour, const QString &min, const QString &sec); }; #endif // IMAGECLOCK_H
完整程式碼
#pragma execution_character_set("utf-8") #include "imageclock.h" #include "qpainter.h" #include "qtimer.h" #include "qdatetime.h" #include "qmath.h" #include "qaction.h" #include "qprocess.h" #include "qdebug.h" ImageClock::ImageClock(QWidget *parent): QWidget(parent) { setFont(QFont("Microsoft Yahei", 9)); QAction *action_trad = new QAction("黑色風格", this); connect(action_trad, SIGNAL(triggered(bool)), this, SLOT(doAction())); this->addAction(action_trad); QAction *action_system = new QAction("銀色風格", this); connect(action_system, SIGNAL(triggered(bool)), this, SLOT(doAction())); this->addAction(action_system); QAction *action_modern = new QAction("紅色風格", this); connect(action_modern, SIGNAL(triggered(bool)), this, SLOT(doAction())); this->addAction(action_modern); QAction *action_flower = new QAction("花瓣風格", this); connect(action_flower, SIGNAL(triggered(bool)), this, SLOT(doAction())); this->addAction(action_flower); action_secondstyle = new QAction("彈簧效果", this); connect(action_secondstyle, SIGNAL(triggered(bool)), this, SLOT(doAction())); this->addAction(action_secondstyle); this->setContextMenuPolicy(Qt::ActionsContextMenu); imageNames << "trad" << "system" << "modern" << "flower"; timer = new QTimer(this); timer->setInterval(1000); connect(timer, SIGNAL(timeout()), this, SLOT(updateTime())); timer->start(); timerSpring = new QTimer(this); timerSpring->setInterval(30); connect(timerSpring, SIGNAL(timeout()), this, SLOT(updateSpring())); angleSpring = 6.0 * (sec + (double)msec / 1000); setClockStyle(ClockStyle_System); setSecondStyle(SecondStyle_Normal); updateTime(); } ImageClock::~ImageClock() { if (timer->isActive()) { timer->stop(); } if (timerSpring->isActive()) { timerSpring->stop(); } } void ImageClock::paintEvent(QPaintEvent *) { int width = this->width(); int height = this->height(); //繪製準備工作,啟用反鋸齒,啟用圖片平滑縮放 QPainter painter(this); painter.setRenderHints(QPainter::Antialiasing | QPainter::TextAntialiasing); painter.setRenderHint(QPainter::SmoothPixmapTransform, true); //繪製背景 drawBg(&painter); painter.translate(width / 2, height / 2); //繪製時鐘指標 尺寸:13×129 drawHour(&painter); //繪製分鐘指標 尺寸:13×129 drawMin(&painter); //繪製秒鐘指標 尺寸:13×129 drawSec(&painter); //繪製中心蓋板 drawDot(&painter); } void ImageClock::drawBg(QPainter *painter) { painter->save(); int pixX = rect().center().x() - clockBg.width() / 2; int pixY = rect().center().y() - clockBg.height() / 2; QPoint point(pixX, pixY); painter->drawImage(point, clockBg); painter->drawImage(point, clockHighlights); painter->restore(); } void ImageClock::drawHour(QPainter *painter) { painter->save(); painter->rotate(30.0 * ((hour + min / 60.0))); painter->drawImage(QRect(-6.5, -64.5, 13, 129), clockHour); painter->restore(); } void ImageClock::drawMin(QPainter *painter) { painter->save(); painter->rotate(6.0 * (min + sec / 60.0)); painter->drawImage(QRect(-6.5, -64.5, 13, 129), clockMin); painter->restore(); } void ImageClock::drawSec(QPainter *painter) { if (secondStyle == SecondStyle_Hide) { return; } painter->save(); painter->rotate(angleSpring); painter->drawImage(QRect(-6.5, -64.5, 13, 129), clockSec); painter->restore(); } void ImageClock::drawDot(QPainter *painter) { painter->save(); painter->drawImage(QRect(-6.5, -64.5, 13, 129), clockDot); painter->restore(); } void ImageClock::doAction() { QAction *action = (QAction *)sender(); QString str = action->text(); if (str == "黑色風格") { setClockStyle(ClockStyle_Trad); } else if (str == "銀色風格") { setClockStyle(ClockStyle_System); } else if (str == "紅色風格") { setClockStyle(ClockStyle_Modern); } else if (str == "花瓣風格") { setClockStyle(ClockStyle_Flower); } else if (str == "彈簧效果") { action->setText("連續效果"); setSecondStyle(SecondStyle_Spring); } else if (str == "連續效果") { action->setText("隱藏效果"); setSecondStyle(SecondStyle_Continue); } else if (str == "隱藏效果") { action->setText("普通效果"); setSecondStyle(SecondStyle_Hide); } else if (str == "普通效果") { action->setText("彈簧效果"); setSecondStyle(SecondStyle_Normal); } } void ImageClock::updateTime() { QTime now = QTime::currentTime(); hour = now.hour(); min = now.minute(); sec = now.second(); msec = now.msec(); if (secondStyle != SecondStyle_Hide) { angleSpring = 6.0 * (sec + (double)msec / 1000); if (secondStyle == SecondStyle_Spring) { angleSpring += 5; timerSpring->start(); } } update(); } void ImageClock::updateSpring() { angleSpring = 6.0 * (sec + (double)msec / 1000); update(); timerSpring->stop(); } ImageClock::ClockStyle ImageClock::getClockStyle() const { return this->clockStyle; } ImageClock::SecondStyle ImageClock::getSecondStyle() const { return this->secondStyle; } QSize ImageClock::sizeHint() const { return QSize(130, 130); } QSize ImageClock::minimumSizeHint() const { return QSize(130, 130); } void ImageClock::setClockStyle(const ClockStyle &clockStyle) { if (this->clockStyle != clockStyle){ QString imageName = imageNames.at(clockStyle); this->clockStyle = clockStyle; clockBg = QImage(QString(":/image/clock_%1.png").arg(imageName)); clockHour = QImage(QString(":/image/clock_%1_h.png").arg(imageName)); clockMin = QImage(QString(":/image/clock_%1_m.png").arg(imageName)); clockSec = QImage(QString(":/image/clock_%1_s.png").arg(imageName)); clockDot = QImage(QString(":/image/clock_%1_dot.png").arg(imageName)); clockHighlights = QImage(QString(":/image/clock_%1_highlights.png").arg(imageName)); update(); } } void ImageClock::setSecondStyle(const SecondStyle &secondStyle) { if (this->secondStyle != secondStyle){ this->secondStyle = secondStyle; if (secondStyle == SecondStyle_Continue) { timer->setInterval(100); } else { timer->setInterval(1000); } if (secondStyle == SecondStyle_Spring) { action_secondstyle->setText("連續效果"); } else if (secondStyle == SecondStyle_Continue) { action_secondstyle->setText("隱藏效果"); } else if (secondStyle == SecondStyle_Hide) { action_secondstyle->setText("普通效果"); } else if (secondStyle == SecondStyle_Normal) { action_secondstyle->setText("彈簧效果"); updateTime(); return; } update(); } } void ImageClock::setSystemDateTime(const QString &year, const QString &month, const QString &day, const QString &hour, const QString &min, const QString &sec) { #ifdef Q_OS_WIN QProcess p(0); p.start("cmd"); p.waitForStarted(); p.write(QString("date %1-%2-%3\n").arg(year).arg(month).arg(day).toLatin1()); p.closeWriteChannel(); p.waitForFinished(1000); p.close(); p.start("cmd"); p.waitForStarted(); p.write(QString("time %1:%2:%3.00\n").arg(hour).arg(min).arg(sec).toLatin1()); p.closeWriteChannel(); p.waitForFinished(1000); p.close(); #else QString cmd = QString("date %1%2%3%4%5.%6").arg(month).arg(day).arg(hour).arg(min).arg(year).arg(sec); system(cmd.toLatin1()); system("hwclock -w"); #endif }
控制元件介紹
- 超過145個精美控制元件,涵蓋了各種儀表盤、進度條、進度球、指南針、曲線圖、標尺、溫度計、導航條、導航欄,flatui、高亮按鈕、滑動選擇器、農曆等。遠超qwt整合的控制元件數量。
- 每個類都可以獨立成一個單獨的控制元件,零耦合,每個控制元件一個頭檔案和一個實現檔案,不依賴其他檔案,方便單個控制元件以原始碼形式整合到專案中,較少程式碼量。qwt的控制元件類環環相扣,高度耦合,想要使用其中一個控制元件,必須包含所有的程式碼。
- 全部純Qt編寫,QWidget+QPainter繪製,支援Qt4.6到Qt5.12的任何Qt版本,支援mingw、msvc、gcc等編譯器,不亂碼,可直接整合到Qt Creator中,和自帶的控制元件一樣使用,大部分效果只要設定幾個屬性即可,極為方便。
- 每個控制元件都有一個對應的單獨的包含該控制元件原始碼的DEMO,方便參考使用。同時還提供一個所有控制元件使用的整合的DEMO。
- 每個控制元件的原始碼都有詳細中文註釋,都按照統一設計規範編寫,方便學習自定義控制元件的編寫。
- 每個控制元件預設配色和demo對應的配色都非常精美。
- 超過130個可見控制元件,6個不可見控制元件。
- 部分控制元件提供多種樣式風格選擇,多種指示器樣式選擇。
- 所有控制元件自適應窗體拉伸變化。
- 整合自定義控制元件屬性設計器,支援拖曳設計,所見即所得,支援匯入匯出xml格式。
- 自帶activex控制元件demo,所有控制元件可以直接執行在ie瀏覽器中。
- 整合fontawesome圖形字型+阿里巴巴iconfont收藏的幾百個圖形字型,享受圖形字型帶來的樂趣。
- 所有控制元件最後生成一個dll動態庫檔案,可以直接整合到qtcreator中拖曳設計使用。
SDK下載
- SDK下載連結:https://pan.baidu.com/s/1tD9v1YPfE2fgYoK6lqUr1Q 提取碼:lyhk
- 自定義控制元件+屬性設計器欣賞:https://pan.baidu.com/s/1l6L3rKSiLu_uYi7lnL3ibQ 提取碼:tmvl
- 下載連結中包含了各個版本的動態庫檔案,所有控制元件的標頭檔案,使用demo。
- 自定義控制元件外掛開放動態庫dll使用(永久免費),無任何後門和限制,請放心使用。
- 目前已提供26個版本的dll,其中包括了qt5.12.3 msvc2017 32+64 mingw 32+64 的。
- 不定期增加控制元件和完善控制元件,不定期更新SDK,歡迎各位提出建議,謝謝!
- widget版本(QQ:517216493)qml版本(QQ:373955953)三峰駝(QQ:278969898)。
相關推薦
Qt編寫自定義控制元件19-圖片背景時鐘
前言 圖片背景時鐘控制元件,是全套控制元件(目前共145個)中唯一的幾個貼圖的控制元件,這個背景要是不貼圖,會畫到猝死,必須用美工
Qt編寫自定義控制元件36-圖片瀏覽器
一、前言 本控制元件主要用來作為一個簡單的圖片瀏覽器使用,可以上下翻頁顯示圖片,圖片還可以開啟過度效果比如透明度漸變,應用場景有檢
Qt編寫自定義控制元件33-圖片切換動畫
一、前言 在很多看圖軟體中,切換圖片的時候可以帶上動畫過渡或者切換效果,顯得更人性化,其實主要還是炫一些,比如百葉窗、透明度變化、
Qt編寫自定義控制元件屬性設計器
以前做.NET開發中,.NET直接就集成了屬性設計器,VS不愧是宇宙第一IDE,你能夠想到的都給你封裝好了,用起來不要太爽!因為專案需要自從全面轉Qt開發已經6年有餘,在工業控制領域,有一些應用場景需要自定義繪製一些控制元件滿足特定的需求,比如儀器儀表、組態等,而且需要直接使用者通過屬性設計的形式生成匯出控制
Qt編寫自定義控制元件一開關按鈕
從2010年進入網際網路+智慧手機時代以來,各種各樣的APP大行其道,手機上面的APP有很多流行的元素,開關按鈕個人非常喜歡,手機QQ、360衛士、金山毒霸等,都有很多開關控制一些操作,在Qt widgets應用專案上,在專案中應用些類似的開關按鈕,估計也會為專案增添不少新鮮
Qt編寫自定義控制元件外掛開放動態庫dll使用(永久免費)
這套控制元件陸陸續續完善了四年多,目前共133個控制元件,除了十幾個控制元件參考網友開源的程式碼寫的,其餘全部原創,在釋出之初就有
Qt編寫自定義控制元件1-汽車儀表盤
前言 汽車儀表盤幾乎是qt寫儀表盤控制元件中最常見的,一般來說先要求美工做好設計圖,然後設計效果圖給到程式設計師,由程式設計師根據
Qt編寫自定義控制元件2-進度條標尺
前言 進度條標尺控制元件的應用場景一般是需要手動拉動進度,上面有標尺可以看到當前進度,類似於qslider控制元件,其實就是qsl
Qt編寫自定義控制元件6-指南針儀表盤
前言 指南針儀表盤,主要用來指示東南西北四個方位,雙向對稱兩個指標旋轉,其實就是360度打轉,功能屬於簡單型,可能指標的繪製稍微難
Qt編寫自定義控制元件7-自定義可拖動多邊形
前言 自定義可拖動多邊形控制元件,原創作者是趙彥博(QQ:408815041 [email protected]),創作之初主要
Qt編寫自定義控制元件9-導航按鈕控制元件
前言 導航按鈕控制元件,主要用於各種漂亮精美的導航條,我們經常在web中看到導航條都非常精美,都是html+css+js實現的,還
Qt編寫自定義控制元件11-裝置防區按鈕控制元件
前言 在很多專案應用中,需要根據資料動態生成物件顯示在地圖上,比如地圖標註,同時還需要可拖動物件到指定位置顯示,能有多種狀態指示,
Qt編寫自定義控制元件12-進度儀表盤
前言 進度儀表盤主要應用場景是標識一個任務進度完成的狀況等,可以自由的設定範圍值和當前值,為了美觀還提供了四種指示器(圓形指示器/
Qt編寫自定義控制元件13-多型進度條
前言 多型進度條,顧名思義,有多重狀態,其實本控制元件主要是用來表示百分比進度的,由於之前已經存在了百分比進度條控制元件,名字被霸
Qt編寫自定義控制元件14-環形進度條
前言 環形進度條,用來展示當前進度,為了滿足大屏UI的需要特意定製,以前有個叫圓環進度條,不能滿足專案需要,只能重新定做,以前的進
Qt編寫自定義控制元件15-百分比儀表盤
前言 百分比儀表盤,主要的應用場景是展示銷售完成率、產品合格率等,也可以作為一個進度百分比展示,可以獨立設定對應的標題文字,標題文
Qt編寫自定義控制元件16-魔法老鼠
前言 五一期間一直忙著大屏電子看板軟體的開發,沒有再去整理控制元件,今天已經將大屏電子看板的所有子視窗都實現了任意停靠和雙擊獨立再
Qt編寫自定義控制元件18-魔法小魚
前言 上次發了個純painter繪製的老鼠,那個就是qt目錄下的demo,改的,只是比demo中的老鼠稍微胖一點,估計人到中年都發
Qt編寫自定義控制元件20-自定義餅圖
一、前言 上次在寫視覺化資料大屏電子看板專案的時候,為了逐步移除對QChart的依賴(主要是因為QChart真的太垃圾了,是所有Q
Qt編寫自定義控制元件28-顏色滑塊面板
一、前言 相比於上一個顏色按鈕面板,此控制元件就要難很多,顏色值有三種表示形式,除了程式設計師最常用的RGB以外,還有HSB和CM