Qt 진도 항목 의 실현 예시
때때로 우 리 는 표(QTableWidget),트 리 표시 줄(QTreeWidget)에서 작업 진 도 를 직관 적 으로 표시 하거나 백분율 을 소모 하여 보고서 표시 형식 에 도달 해 야 하 며,QLabel 을 다시 쓰 는 방식 으로 이 루어 질 수 있다.
1.진도 바 컨트롤 기능
1)설정 가능 한 값 동적 변화
2)경계 치 설정 가능
3)정상 색상 과 경고 색상 설정 가능
4)테두리 그 라 데 이 션 색상 설정 가능
5)변화 할 때마다 이동 하 는 보폭 설정 가능
6)오류 설정 시 오류 설명 표시
7)디 스 플레이 값 을 소수 로 유지 할 수 있 는 자릿수 를 설정 할 수 있다.
8)테두리 원 각 각도/배경 진도 원 각 각도/머리 원 각 각도 설정 가능
2.실현 효과
2.실현 과정
1.운영 환경 Qt 5.5 VS 2013
2,계승 QLabel 재 작성 ProgressLabel 컨트롤
/***********************************************************************
:liangtianmanyue(QQ:1660941209) 2021-05-30
:
1、
2、
3、
4、
5、
6、
7、
8、 / /
************************************************************************/
#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:
//
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 setAlarmValue(double alarmValue);
void setAlarmValue(int alarmValue);
//
void setStep(double step);
void setStep(int step);
//
int getDecimals();
void setDecimals(int decimals);
//
void setBorderRadius(int borderRadius);
//
void setBgRadius(int bgRadius);
//
void setHeadRadius(int headRadius);
//
void setBorderColorStart(const QColor &borderColorStart);
void setBorderColorEnd(const QColor &borderColorEnd);
//
void setAlarmColorStart(const QColor &alarmColorStart);
void setAlarmColorEnd(const QColor &alarmColorEnd);
//
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; //
QString m_ErrorText; //
double minValue; //
double maxValue; //
double value; //
double alarmValue; //
int decimals; //
double step; //
int borderRadius; //
int bgRadius; //
int headRadius; //
QColor borderColorStart; //
QColor borderColorEnd; //
QColor alarmColorStart; //
QColor alarmColorEnd; //
QColor normalColorStart; //
QColor normalColorEnd; //
bool isForward; //
double currentValue; //
QRectF mainRect; //
QTimer *timer; //
};
#endif // PROGRESS_LABEL_H
3.paintEvent 이 벤트 를 다시 작성 하고 오류 여부 에 따라 오류 정보 나 값 을 그립 니 다.
void ProgressLabel::paintEvent(QPaintEvent *)
{
// ,
QPainter painter(this);
painter.setRenderHints(QPainter::Antialiasing | QPainter::TextAntialiasing);
//
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();
}
//
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.새로 고침 시 타이머 정시 새로 고침 방식 을 사용 하여 동적 효 과 를 얻는다.타이머 생 성
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;//
}
}
else
{
currentValue += step;
if (currentValue >= value)
{
timer->stop();
currentValue = value;//
}
}
this->update();
}
5.외부 설정 값 에서 오류 플래그 를 지우 고 타이머 시작
void ProgressLabel::setValue(double value)
{
m_IsError = false;
//
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();
}
여기에 Qt 진도 항목 의 실현 예시 에 관 한 이 글 은 여기까지 소개 되 었 습 니 다.더 많은 Qt 진도 항목 내용 은 우리 의 이전 글 을 검색 하거나 아래 의 관련 글 을 계속 조회 하 시기 바 랍 니 다.앞으로 많은 응원 바 랍 니 다!