一、前言
這個控件寫了很久了,是最早期的一批控件中的一個,和溫度計控件類似,都是垂直的進度條,可以設定不同的背景顔色,左側的刻度也可以自由設定,還提供了動畫效果,其實就是開啟定時器慢慢的進度到設定的目标值,如果設定的值比目前值大,則遞增,反之遞減。由于當時的qpainter繪制功底還不夠如火純情,是以當時的刻度尺部分都是定死的字型大小,并不會随着控件變化而增大。
二、實作的功能
- 1:可設定精确度(小數點後幾位)和間距
- 2:可設定背景色/柱狀顔色/線條顔色
- 3:可設定長線條步長及短線條步長
- 4:可啟用動畫及設定動畫步長
- 5:可設定範圍值
- 6:支援負數刻度值
三、效果圖
![](https://img.laitimes.com/img/9ZDMuAjOiMmIsIjOiQnIsIiZpdmLzIDNwQWNyETYkVWZiRzM4UDZmZWNyIGMxIGO3ETZzQGN5UjN4ETZy8CXt92Yu4GZjlGbh5SZslmZxl3Lc9CX6MHc0RHaiojIsJye.gif)
四、頭檔案代碼
#ifndef RULERBAR_H
#define RULERBAR_H
/**
* 柱狀标尺控件 作者:feiyangqingyun(QQ:517216493) 2016-10-28
* 本控件來源于網絡(原作者:kimtaikee(http://www.qtcn.org/bbs/read-htm-tid-33693-ds-1.html#tpc))
* 1:可設定精确度(小數點後幾位)和間距
* 2:可設定背景色/柱狀顔色/線條顔色
* 3:可設定長線條步長及短線條步長
* 4:可啟用動畫及設定動畫步長
* 5:可設定範圍值
* 6:支援負數刻度值
*/
#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 RulerBar : public QWidget
#else
class RulerBar : public QWidget
#endif
{
Q_OBJECT
Q_PROPERTY(double minValue READ getMinValue WRITE setMinValue)
Q_PROPERTY(double maxValue READ getMaxValue WRITE setMaxValue)
Q_PROPERTY(double value READ getValue WRITE setValue)
Q_PROPERTY(int precision READ getPrecision WRITE setPrecision)
Q_PROPERTY(int longStep READ getLongStep WRITE setLongStep)
Q_PROPERTY(int shortStep READ getShortStep WRITE setShortStep)
Q_PROPERTY(int space READ getSpace WRITE setSpace)
Q_PROPERTY(bool animation READ getAnimation WRITE setAnimation)
Q_PROPERTY(double animationStep READ getAnimationStep WRITE setAnimationStep)
Q_PROPERTY(QColor bgColorStart READ getBgColorStart WRITE setBgColorStart)
Q_PROPERTY(QColor bgColorEnd READ getBgColorEnd WRITE setBgColorEnd)
Q_PROPERTY(QColor lineColor READ getLineColor WRITE setLineColor)
Q_PROPERTY(QColor barBgColor READ getBarBgColor WRITE setBarBgColor)
Q_PROPERTY(QColor barColor READ getBarColor WRITE setBarColor)
public:
explicit RulerBar(QWidget *parent = 0);
~RulerBar();
protected:
void paintEvent(QPaintEvent *);
void drawBg(QPainter *painter);
void drawRuler(QPainter *painter);
void drawBarBg(QPainter *painter);
void drawBar(QPainter *painter);
private:
double minValue; //最小值
double maxValue; //最大值
double value; //目标值
int precision; //精确度,小數點後幾位
int longStep; //長線條等分步長
int shortStep; //短線條等分步長
int space; //間距
bool animation; //是否啟用動畫顯示
double animationStep; //動畫顯示時步長
QColor bgColorStart; //背景漸變開始顔色
QColor bgColorEnd; //背景漸變結束顔色
QColor lineColor; //線條顔色
QColor barBgColor; //柱狀背景色
QColor barColor; //柱狀顔色
bool reverse; //是否倒退
double currentValue; //目前值
QTimer *timer; //定時器繪制動畫
QRectF barRect; //柱狀圖區域
private slots:
void updateValue();
public:
double getMinValue() const;
double getMaxValue() const;
double getValue() const;
int getPrecision() const;
int getLongStep() const;
int getShortStep() const;
int getSpace() const;
bool getAnimation() const;
double getAnimationStep() const;
QColor getBgColorStart() const;
QColor getBgColorEnd() const;
QColor getLineColor() const;
QColor getBarBgColor() const;
QColor getBarColor() const;
QSize sizeHint() const;
QSize minimumSizeHint() const;
public Q_SLOTS:
//設定最大最小值-範圍值
void setRange(double minValue, double maxValue);
void setRange(int minValue, int maxValue);
//設定最大最小值
void setMinValue(double minValue);
void setMaxValue(double maxValue);
//設定目标值
void setValue(double value);
void setValue(int value);
//設定精确度
void setPrecision(int precision);
//設定線條等分步長
void setLongStep(int longStep);
void setShortStep(int shortStep);
//設定間距
void setSpace(int space);
//設定是否啟用動畫顯示
void setAnimation(bool animation);
//設定動畫顯示的步長
void setAnimationStep(double animationStep);
//設定背景顔色
void setBgColorStart(const QColor &bgColorStart);
void setBgColorEnd(const QColor &bgColorEnd);
//設定線條顔色
void setLineColor(const QColor &lineColor);
//設定柱狀顔色
void setBarBgColor(const QColor &barBgColor);
void setBarColor(const QColor &barColor);
Q_SIGNALS:
void valueChanged(double value);
};
#endif // RULERBAR_H
五、核心代碼
void RulerBar::paintEvent(QPaintEvent *)
{
//繪制準備工作,啟用反鋸齒
QPainter painter(this);
painter.setRenderHints(QPainter::Antialiasing | QPainter::TextAntialiasing);
//繪制背景
drawBg(&painter);
//繪制标尺
drawRuler(&painter);
//繪制柱狀背景
drawBarBg(&painter);
//繪制柱狀
drawBar(&painter);
}
void RulerBar::drawBg(QPainter *painter)
{
painter->save();
painter->setPen(Qt::NoPen);
QLinearGradient bgGradient(QPointF(0, 0), QPointF(0, height()));
bgGradient.setColorAt(0.0, bgColorStart);
bgGradient.setColorAt(1.0, bgColorEnd);
painter->setBrush(bgGradient);
painter->drawRect(rect());
painter->restore();
}
void RulerBar::drawRuler(QPainter *painter)
{
painter->save();
painter->setPen(lineColor);
//繪制縱向标尺線 20的長度為刻度尺文字的寬度
double initX = space + 20;
double initY = space;
QPointF topPot(initX, initY);
QPointF bottomPot(initX, height() - space);
painter->drawLine(topPot, bottomPot);
//繪制縱向标尺刻度
double length = height() - 2 * space;
//計算每一格移動多少
double increment = length / (maxValue - minValue);
//長線條短線條長度
int longLineLen = 10;
int shortLineLen = 7;
//根據範圍值繪制刻度值及刻度值
for (int i = maxValue; i >= minValue; i = i - shortStep) {
if (i % longStep == 0) {
QPointF leftPot(initX + longLineLen, initY);
QPointF rightPot(initX, initY);
painter->drawLine(leftPot, rightPot);
QString strValue = QString("%1").arg((double)i, 0, 'f', precision);
double fontWidth = painter->fontMetrics().width(strValue);
double fontHeight = painter->fontMetrics().height();
QPointF textPot(initX - fontWidth - 5, initY + fontHeight / 3);
painter->drawText(textPot, strValue);
} else {
if (i % (longStep / 2) == 0) {
shortLineLen = 7;
} else {
shortLineLen = 4;
}
QPointF leftPot(initX + shortLineLen, initY);
QPointF rightPot(initX, initY);
painter->drawLine(leftPot, rightPot);
}
initY += increment * shortStep;
}
painter->restore();
}
void RulerBar::drawBarBg(QPainter *painter)
{
painter->save();
painter->setPen(Qt::NoPen);
//20的長度為刻度尺文字的寬度 15為刻度尺到柱狀圖的寬度
double initX = space + 20 + 15;
QPointF topLeftPot(initX, space);
QPointF bottomRightPot(width() - space , height() - space);
barRect = QRectF(topLeftPot, bottomRightPot);
painter->setBrush(barBgColor);
painter->drawRect(barRect);
painter->restore();
}
void RulerBar::drawBar(QPainter *painter)
{
painter->save();
painter->setPen(Qt::NoPen);
double barHeight = barRect.height();
double increment = (double)barHeight / (maxValue - minValue);
double initY = (currentValue - minValue) * increment;
QPointF topLeftPot(barRect.topLeft().x(), barRect.bottomLeft().y() - initY);
QPointF bottomRightPot(barRect.bottomRight());
QRectF currentRect(topLeftPot, bottomRightPot);
painter->setBrush(barColor);
painter->drawRect(currentRect);
painter->restore();
}
六、控件介紹
- 超過150個精美控件,涵蓋了各種儀表盤、進度條、進度球、指南針、曲線圖、标尺、溫度計、導覽列、導航欄,flatui、高亮按鈕、滑動選擇器、農曆等。遠超qwt內建的控件數量。
- 每個類都可以獨立成一個單獨的控件,零耦合,每個控件一個頭檔案和一個實作檔案,不依賴其他檔案,友善單個控件以源碼形式內建到項目中,較少代碼量。qwt的控件類環環相扣,高度耦合,想要使用其中一個控件,必須包含所有的代碼。
- 全部純Qt編寫,QWidget+QPainter繪制,支援Qt4.6到Qt5.12的任何Qt版本,支援mingw、msvc、gcc等編譯器,支援任意作業系統比如windows+linux+mac+嵌入式linux等,不亂碼,可直接內建到Qt Creator中,和自帶的控件一樣使用,大部分效果隻要設定幾個屬性即可,極為友善。
- 每個控件都有一個對應的單獨的包含該控件源碼的DEMO,友善參考使用。同時還提供一個所有控件使用的內建的DEMO。
- 每個控件的源代碼都有詳細中文注釋,都按照統一設計規範編寫,友善學習自定義控件的編寫。
- 每個控件預設配色和demo對應的配色都非常精美。
- 超過130個可見控件,6個不可見控件。
- 部分控件提供多種樣式風格選擇,多種訓示器樣式選擇。
- 所有控件自适應窗體拉伸變化。
- 內建自定義控件屬性設計器,支援拖曳設計,所見即所得,支援導入導出xml格式。
- 自帶activex控件demo,所有控件可以直接運作在ie浏覽器中。
- 內建fontawesome圖形字型+阿裡巴巴iconfont收藏的幾百個圖形字型,享受圖形字型帶來的樂趣。
- 所有控件最後生成一個dll動态庫檔案,可以直接內建到qtcreator中拖曳設計使用。
- 目前已經有qml版本,後期會考慮出pyqt版本,如果使用者需求量很大的話。
七、SDK下載下傳
- SDK下載下傳連結: https://pan.baidu.com/s/1A5Gd77kExm8Co5ckT51vvQ 提取碼:877p
- 下載下傳連結中包含了各個版本的動态庫檔案,所有控件的頭檔案,使用demo,自定義控件+屬性設計器。
- 自定義控件插件開放動态庫dll使用(永久免費),無任何後門和限制,請放心使用。
- 目前已提供26個版本的dll,其中包括了qt5.12.3 msvc2017 32+64 mingw 32+64 的。
- 不定期增加控件和完善控件,不定期更新SDK,歡迎各位提出建議,謝謝!
- widget版本(QQ:517216493)qml版本(QQ:373955953)三峰駝(QQ:278969898)。
- 濤哥的知乎專欄 Qt進階之路 https://zhuanlan.zhihu.com/TaoQt
- 歡迎關注微信公衆号【高效程式員】,C++/Python、學習方法、寫作技巧、熱門技術、職場發展等内容,幹貨多多,福利多多!
- Qt入門書籍推薦霍亞飛的《Qt Creator快速入門》《Qt5程式設計入門》,Qt進階書籍推薦官方的《C++ GUI Qt4程式設計》。
- 強烈推薦程式員自我修養和規劃系列書《大話程式員》《程式員的成長課》《解憂程式員》!