前言
按鈕進度條,顧名思義,表面上長得像一個按鈕,單擊以后切換成進度條指示按鈕單擊動作執行的進度,主要用在一些需要直接在按鈕執行動作顯示對應進度的場景,在很多網頁中經常看到這種效果,這個效果有個優點就是直接在原地顯示進度條,不占用其他位置,然后提供各種顏色可以設置。近期大屏電子看板程序接近尾聲了,文章末尾貼出幾張動圖效果。
實現的功能
- 1:可設置進度線條寬度+顏色
- 2:可設置邊框寬度+顏色
- 3:可設置圓角角度+背景顏色
效果圖
頭文件代碼
#ifndef PROGRESSBUTTON_H
#define PROGRESSBUTTON_H
/**
* 按鈕進度條控件 作者:倪大俠(QQ:393320854 zyb920@hotmail.com) 2019-4-17
* 1:可設置進度線條寬度+顏色
* 2:可設置邊框寬度+顏色
* 3:可設置圓角角度+背景顏色
*/
#include <QWidget>
class QTimer;
#ifdef quc
#if (QT_VERSION < QT_VERSION_CHECK(5,7,0))
#include <QtDesigner/QDesignerExportWidget>
#else
#include <QtUiPlugin/QDesignerExportWidget>
#endif
class QDESIGNER_WIDGET_EXPORT ProgressButton : public QWidget
#else
class ProgressButton : public QWidget
#endif
{
Q_OBJECT
Q_PROPERTY(int lineWidth READ getLineWidth WRITE setLineWidth)
Q_PROPERTY(QColor lineColor READ getLineColor WRITE setLineColor)
Q_PROPERTY(int borderWidth READ getBorderWidth WRITE setBorderWidth)
Q_PROPERTY(QColor borderColor READ getBorderColor WRITE setBorderColor)
Q_PROPERTY(int borderRadius READ getBorderRadius WRITE setBorderRadius)
Q_PROPERTY(QColor bgColor READ getBgColor WRITE setBgColor)
public:
explicit ProgressButton(QWidget *parent = 0);
protected:
void resizeEvent(QResizeEvent *);
void mousePressEvent(QMouseEvent *);
void paintEvent(QPaintEvent *);
void drawBg(QPainter *painter);
void drawProgress(QPainter *painter);
private:
int lineWidth; //線條寬度
QColor lineColor; //線條顏色
int borderWidth; //邊框寬度
QColor borderColor; //邊框顏色
int borderRadius; //圓角角度
QColor bgColor; //背景顏色
double value; //當前值
int status; //狀態
int tempWidth; //動態改變寬度
QTimer *timer; //定時器改變進度
public:
int getLineWidth() const;
QColor getLineColor() const;
int getBorderWidth() const;
QColor getBorderColor() const;
int getBorderRadius() const;
QColor getBgColor() const;
QSize sizeHint() const;
QSize minimumSizeHint() const;
private slots:
void progress();
public Q_SLOTS:
//設置線條寬度+顏色
void setLineWidth(int lineWidth);
void setLineColor(const QColor &lineColor);
//設置邊框寬度+顏色
void setBorderWidth(int borderWidth);
void setBorderColor(const QColor &borderColor);
//設置圓角角度+背景顏色
void setBorderRadius(int borderRadius);
void setBgColor(const QColor &bgColor);
Q_SIGNALS:
void valueChanged(int value);
};
#endif // PROGRESSBUTTON_H
核心代碼
void ProgressButton::paintEvent(QPaintEvent *)
{
QPainter painter(this);
painter.setRenderHints(QPainter::Antialiasing | QPainter::TextAntialiasing);
if (1 == status) {
//繪制當前進度
drawProgress(&painter);
} else {
//繪制按鈕背景
drawBg(&painter);
}
}
void ProgressButton::drawBg(QPainter *painter)
{
painter->save();
int width = this->width();
int height = this->height();
int side = qMin(width, height);
QPen pen;
pen.setWidth(borderWidth);
pen.setColor(borderColor);
painter->setPen(borderWidth > 0 ? pen : Qt::NoPen);
painter->setBrush(bgColor);
QRect rect(((width - tempWidth) / 2) + borderWidth, borderWidth, tempWidth - (borderWidth * 2), height - (borderWidth * 2));
painter->drawRoundedRect(rect, borderRadius, borderRadius);
QFont font;
font.setPixelSize(side - 18);
painter->setFont(font);
painter->setPen(lineColor);
painter->drawText(rect, Qt::AlignCenter, status == 2 ? "完 成" : "開 始");
painter->restore();
}
void ProgressButton::drawProgress(QPainter *painter)
{
painter->save();
int width = this->width();
int height = this->height();
int side = qMin(width, height);
int radius = 99 - borderWidth;
//繪制外圓
QPen pen;
pen.setWidth(borderWidth);
pen.setColor(borderColor);
painter->setPen(borderWidth > 0 ? pen : Qt::NoPen);
painter->setBrush(bgColor);
//平移坐標軸中心,等比例縮放
QRect rectCircle(-radius, -radius, radius * 2, radius * 2);
painter->translate(width / 2, height / 2);
painter->scale(side / 200.0, side / 200.0);
painter->drawEllipse(rectCircle);
//繪制圓弧進度
pen.setWidth(lineWidth);
pen.setColor(lineColor);
painter->setPen(pen);
int offset = radius - lineWidth - 5;
QRectF rectArc(-offset, -offset, offset * 2, offset * 2);
int startAngle = offset * 16;
int spanAngle = -value * 16;
painter->drawArc(rectArc, startAngle, spanAngle);
//繪制進度文字
QFont font;
font.setPixelSize(offset - 15);
painter->setFont(font);
QString strValue = QString("%1%").arg((int)value * 100 / 360);
painter->drawText(rectCircle, Qt::AlignCenter, strValue);
painter->restore();
}
void ProgressButton::progress()
{
if (0 == status) {
tempWidth -= 5;
if (tempWidth < this->height() / 2) {
tempWidth = this->height() / 2;
status = 1;
}
} else if (1 == status) {
value += 1.0;
if (value >= 360) {
value = 360.0;
status = 2;
}
} else if (2 == status) {
tempWidth += 5;
if (tempWidth > this->width()) {
tempWidth = this->width();
timer->stop();
}
}
this->update();
}
控件介紹
- 超過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)。