Qt 進(jìn)度條的實(shí)現(xiàn)示例
一、前言
有時(shí)我們需要在表格(QTableWidget)、樹狀欄(QTreeWidget)中直觀顯示任務(wù)進(jìn)度或消耗百分比,達(dá)到報(bào)表顯示的形式,可通過重寫QLabel的方式實(shí)現(xiàn)。
1、進(jìn)度條控件功能
1)可設(shè)置值動態(tài)變化
2)可設(shè)置警戒值
3)可設(shè)置正常顏色和報(bào)警顏色
4)可設(shè)置邊框漸變顏色
5)可設(shè)置變化時(shí)每次移動的步長
6)可設(shè)置錯(cuò)誤時(shí)顯示錯(cuò)誤描述
7)可設(shè)置顯示值保留小數(shù)的位數(shù)
8)可設(shè)置邊框圓角角度/背景進(jìn)度圓角角度/頭部圓角角度
2、實(shí)現(xiàn)效果

二、實(shí)現(xiàn)過程
1、運(yùn)行環(huán)境Qt5.5 VS2013
2、繼承QLabel重寫ProgressLabel控件
/***********************************************************************
作者:liangtianmanyue(QQ:1660941209) 2021-05-30
功能:進(jìn)度控件
1、可設(shè)置值動態(tài)變化
2、可設(shè)置警戒值
3、可設(shè)置正常顏色和報(bào)警顏色
4、可設(shè)置邊框漸變顏色
5、可設(shè)置變化時(shí)每次移動的步長
6、可設(shè)置錯(cuò)誤時(shí)顯示錯(cuò)誤描述
7、可設(shè)置顯示值保留小數(shù)的位數(shù)
8、可設(shè)置邊框圓角角度/背景進(jìn)度圓角角度/頭部圓角角度
************************************************************************/
#ifndef PROGRESS_LABEL_H
#define PROGRESS_LABEL_H
#include <QLabel>
#include <QWidget>
#ifdef Plugin
#if (QT_VERSION < QT_VERSION_CHECK(5,7,0))
#include <QtDesigner/QDesignerExportWidget>
#else
#include <QtUiPlugin/QDesignerExportWidget>
#endif
class QDESIGNER_WIDGET_EXPORT ProgressLabel : public QLabel
#else
class ProgressLabel : public QLabel
#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(double alarmValue READ getAlarmValue WRITE setAlarmValue)
Q_PROPERTY(double step READ getStep WRITE setStep)
Q_PROPERTY(int decimals READ getDecimals WRITE setDecimals)
Q_PROPERTY(int borderRadius READ getBorderRadius WRITE setBorderRadius)
Q_PROPERTY(int bgRadius READ getBgRadius WRITE setBgRadius)
Q_PROPERTY(int headRadius READ getHeadRadius WRITE setHeadRadius)
Q_PROPERTY(QColor borderColorStart READ getBorderColorStart WRITE setBorderColorStart)
Q_PROPERTY(QColor borderColorEnd READ getBorderColorEnd WRITE setBorderColorEnd)
Q_PROPERTY(QColor alarmColorStart READ getAlarmColorStart WRITE setAlarmColorStart)
Q_PROPERTY(QColor alarmColorEnd READ getAlarmColorEnd WRITE setAlarmColorEnd)
Q_PROPERTY(QColor normalColorStart READ getNormalColorStart WRITE setNormalColorStart)
Q_PROPERTY(QColor normalColorEnd READ getNormalColorEnd WRITE setNormalColorEnd)
public:
explicit ProgressLabel(QWidget *parent = 0);
~ProgressLabel();
protected:
void paintEvent(QPaintEvent *);
void drawBg(QPainter *painter);
private slots:
void updateValue();
public:
double getMinValue() const;
double getMaxValue() const;
double getValue() const;
double getAlarmValue() const;
double getStep() const;
int getBorderRadius() const;
int getBgRadius() const;
int getHeadRadius() const;
QColor getBorderColorStart() const;
QColor getBorderColorEnd() const;
QColor getAlarmColorStart() const;
QColor getAlarmColorEnd() const;
QColor getNormalColorStart() const;
QColor getNormalColorEnd() const;
QSize sizeHint() const;
QSize minimumSizeHint() const;
public Q_SLOTS:
//設(shè)置范圍值
void setRange(double minValue, double maxValue);
void setRange(int minValue, int maxValue);
//設(shè)置最大最小值
void setMinValue(double minValue);
void setMaxValue(double maxValue);
//設(shè)置顯示值
void setValue(double value);
void setValue(int value);
//設(shè)置警戒值
void setAlarmValue(double alarmValue);
void setAlarmValue(int alarmValue);
//設(shè)置步長
void setStep(double step);
void setStep(int step);
//小數(shù)點(diǎn)位數(shù)
int getDecimals();
void setDecimals(int decimals);
//設(shè)置邊框圓角角度
void setBorderRadius(int borderRadius);
//設(shè)置背景圓角角度
void setBgRadius(int bgRadius);
//設(shè)置頭部圓角角度
void setHeadRadius(int headRadius);
//設(shè)置邊框漸變顏色
void setBorderColorStart(const QColor &borderColorStart);
void setBorderColorEnd(const QColor &borderColorEnd);
//設(shè)置報(bào)警時(shí)的漸變顏色
void setAlarmColorStart(const QColor &alarmColorStart);
void setAlarmColorEnd(const QColor &alarmColorEnd);
//設(shè)置正常時(shí)的漸變顏色
void setNormalColorStart(const QColor &normalColorStart);
void setNormalColorEnd(const QColor &normalColorEnd);
//正常、異常顯示
void setNormalState();
void setErrorText(const QString &text);
Q_SIGNALS:
void valueChanged(double value);
private:
bool m_IsError; //是否出錯(cuò)
QString m_ErrorText; //錯(cuò)誤描述
double minValue; //最小值
double maxValue; //最大值
double value; //目標(biāo)電量
double alarmValue; //警戒值
int decimals; //顯示小數(shù)點(diǎn)后位數(shù)
double step; //每次移動的步長
int borderRadius; //邊框圓角角度
int bgRadius; //背景進(jìn)度圓角角度
int headRadius; //頭部圓角角度
QColor borderColorStart; //邊框漸變開始顏色
QColor borderColorEnd; //邊框漸變結(jié)束顏色
QColor alarmColorStart; //超警戒值時(shí)的漸變開始顏色
QColor alarmColorEnd; //超警戒值時(shí)的漸變結(jié)束顏色
QColor normalColorStart; //正常時(shí)的漸變開始顏色
QColor normalColorEnd; //正常時(shí)的漸變結(jié)束顏色
bool isForward; //是否往前移
double currentValue; //當(dāng)前值
QRectF mainRect; //主體區(qū)域
QTimer *timer; //繪制定時(shí)器
};
#endif // PROGRESS_LABEL_H
3、重寫paintEvent事件,根據(jù)是否有出錯(cuò),繪制出錯(cuò)信息或值
void ProgressLabel::paintEvent(QPaintEvent *)
{
//繪制準(zhǔn)備工作,啟用反鋸齒
QPainter painter(this);
painter.setRenderHints(QPainter::Antialiasing | QPainter::TextAntialiasing);
//獲取邊框區(qū)域
QPointF topLeft(2, 2);
QPointF bottomRight(width() - 4, height() - 2);
mainRect = QRectF(topLeft, bottomRight);
//繪制背景
drawBg(&painter);
}
void ProgressLabel::drawBg(QPainter *painter)
{
if(!m_IsError)
{
painter->save();
QLinearGradient gradient(QPointF(0, 0), QPointF(0, height()));
if (currentValue >= alarmValue)
{
gradient.setColorAt(0.0, alarmColorStart);
gradient.setColorAt(1.0, alarmColorEnd);
}
else
{
gradient.setColorAt(0.0, normalColorStart);
gradient.setColorAt(1.0, normalColorEnd);
}
double min = qMin(width(), height());
int margin = min / 20;
double unit = (mainRect.width() - (margin * 2)) / 100;
double width = currentValue * unit;
QPointF topLeft(mainRect.topLeft().x() + margin, mainRect.topLeft().y() + margin);
QPointF bottomRight(width + margin + , mainRect.bottomRight().y() - margin);
QRectF rect(topLeft, bottomRight);
painter->setPen(Qt::NoPen);
painter->setBrush(gradient);
painter->drawRoundedRect(rect, bgRadius, bgRadius);
painter->restore();
}
//寫進(jìn)度
painter->save();
QPen pen(Qt::SolidLine);
pen.setWidth(1);
if(m_IsError)
pen.setColor(Qt::red);
else
pen.setColor(Qt::black);
painter->setPen(pen);
painter->setBrush(Qt::NoBrush);
if(m_IsError)
painter->drawText(mainRect, Qt::AlignCenter, m_ErrorText);
else
painter->drawText(mainRect, Qt::AlignCenter, QString("%1%").arg(currentValue, 0, 'f', decimals));
painter->restore();
}
4、刷新值時(shí)采用定時(shí)器定時(shí)刷新方式,達(dá)到動態(tài)效果
創(chuàng)建定時(shí)器
timer = new QTimer(this); timer->setInterval(10); connect(timer, SIGNAL(timeout()), this, SLOT(updateValue()));
按step值刷新
void ProgressLabel::updateValue()
{
if (isForward)
{
currentValue -= step;
if (currentValue <= value)
{
timer->stop();
currentValue = value;//保持真實(shí)性
}
}
else
{
currentValue += step;
if (currentValue >= value)
{
timer->stop();
currentValue = value;//保持真實(shí)性
}
}
this->update();
}
5、外部設(shè)置值的時(shí)候,清除錯(cuò)誤標(biāo)志,并啟動定時(shí)器
void ProgressLabel::setValue(double value)
{
m_IsError = false;
//值和當(dāng)前值一致則無需處理
if (value == this->value)
return;
//值小于最小值則取最小值,大于最大值則取最大值
if (value < minValue)
value = minValue;
else if (value > maxValue)
value = maxValue;
if (value > currentValue)
isForward = false;
else if (value < currentValue)
isForward = true;
else
return;
this->value = value;
this->update();
emit valueChanged(value);
timer->start();
}
到此這篇關(guān)于Qt 進(jìn)度條的實(shí)現(xiàn)示例的文章就介紹到這了,更多相關(guān)Qt 進(jìn)度條內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
C++第11版本中的一些強(qiáng)大的新特性小結(jié)
這篇文章主要介紹了C++第11版本中的一些強(qiáng)大的新特性小結(jié),需要的朋友可以參考下2015-12-12
C++指針運(yùn)算符(&和*)的實(shí)現(xiàn)
C++ 提供了兩種指針運(yùn)算符,一種是取地址運(yùn)算符 &,一種是間接尋址運(yùn)算符 *,本文就詳細(xì)的介紹一下這兩種運(yùn)算符的使用,具有一定的參考價(jià)值,感興趣的可以了解一下2023-08-08
C指針原理教程之編譯原理-小型計(jì)算器實(shí)現(xiàn)
本文給大家分享的是如何使用C語言編寫一個(gè)小型計(jì)算器的實(shí)例代碼,有需要的小伙伴可以參考下2019-02-02
C語言數(shù)組和指針,內(nèi)存之間的關(guān)系
這篇文章主要介紹了C語言數(shù)組和指針,內(nèi)存之間的關(guān)系,首先論證一維數(shù)組和一級指針之前的關(guān)系,我們常常使用一級指針指針的方式訪問一維數(shù)組,只有對內(nèi)存的理解到位才能理解它們直接的關(guān)系。需要的小伙伴可以參考一下2022-02-02
C++詳解使用floor&ceil&round實(shí)現(xiàn)保留小數(shù)點(diǎn)后兩位
這篇文章主要介紹了C++使用floor&ceil&round實(shí)現(xiàn)保留小數(shù)點(diǎn)后兩位的方法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-07-07
解決Visual?Studio?Code錯(cuò)誤Cannot?build?and?debug?because?
這篇文章主要為大家介紹了解決Visual?Studio?Code錯(cuò)誤Cannot?build?and?debug?because?the及分析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-07-07

