导航进度条控件,其实就是支付宝、京东、淘宝订单页面的进度控件,提示当前第几步,总共有几步,而后当前进度特殊颜色显示,每一个进度带有时间文字等信息,本控件特地将三种样式风格都集成进去了,京东订单流程样式/淘宝订单流程样式/支付宝订单流程样式,能够动态切换样式,控件自适应任何分辨率,能够自由调整自身大小以适应分辨率的改变,总步骤以及当前步骤都是自动计算占用区域比例,直接提供接口设置步骤对应的文字信息等,接口很是友好。linux
#ifndef NAVPROGRESS_H #define NAVPROGRESS_H /** * 导航进度条控件 做者:feiyangqingyun(QQ:517216493) 2016-11-29 * 1:可设置前景色/背景色/当前值前景色/当前值背景色 * 2:可设置最大步数及当前第几步 * 3:可设置导航标签队列文字信息 * 4:可设置三种风格样式 京东订单流程样式/淘宝订单流程样式/支付宝订单流程样式 * 5:文字自适应大小 */ #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 NavProgress : public QWidget #else class NavProgress : public QWidget #endif { Q_OBJECT Q_ENUMS(NavStyle) Q_PROPERTY(int maxStep READ getMaxStep WRITE setMaxStep) Q_PROPERTY(int currentStep READ getCurrentStep WRITE setCurrentStep) Q_PROPERTY(NavStyle navStyle READ getNavStyle WRITE setNavStyle) Q_PROPERTY(QColor background READ getBackground WRITE setBackground) Q_PROPERTY(QColor foreground READ getForeground WRITE setForeground) Q_PROPERTY(QColor currentBackground READ getCurrentBackground WRITE setCurrentBackground) Q_PROPERTY(QColor currentForeground READ getCurrentForeground WRITE setCurrentForeground) public: enum NavStyle { NavStyle_JD = 0, //京东订单流程样式 NavStyle_TB = 1, //淘宝订单流程样式 NavStyle_ZFB = 2 //支付宝订单流程样式 }; explicit NavProgress(QWidget *parent = 0); protected: void paintEvent(QPaintEvent *); void drawBg_JD(QPainter *painter); void drawText_JD(QPainter *painter); void drawCurrentBg_JD(QPainter *painter); void drawCurrentText_JD(QPainter *painter); void drawBg_TB(QPainter *painter); void drawText_TB(QPainter *painter); void drawCurrentBg_TB(QPainter *painter); void drawBg_ZFB(QPainter *painter); void drawText_ZFB(QPainter *painter); void drawCurrentBg_ZFB(QPainter *painter); private: QStringList topInfo; //导航顶部标签数据 QStringList bottomInfo; //导航底部标签数据 int maxStep; //最大步数 int currentStep; //当前第几步 NavStyle navStyle; //导航样式 QColor background; //背景色 QColor foreground; //前景色 QColor currentBackground; //当前背景色 QColor currentForeground; //当前前景色 QFont iconFont; //图形字体 public: QStringList getTopInfo() const; QStringList getBottomInfo() const; int getMaxStep() const; int getCurrentStep() const; NavStyle getNavStyle() const; QColor getBackground() const; QColor getForeground() const; QColor getCurrentBackground() const; QColor getCurrentForeground() const; QSize sizeHint() const; QSize minimumSizeHint() const; public Q_SLOTS: //设置导航顶部标签数据 void setTopInfo(const QStringList &topInfo); //设置导航底部标签数据 void setBottomInfo(const QStringList &bottomInfo); //设置最大步数 void setMaxStep(int maxStep); //设置当前第几步 void setCurrentStep(int currentStep); //设置导航样式 void setNavStyle(const NavStyle &navStyle); //设置前景色 void setBackground(const QColor &background); //设置前景色 void setForeground(const QColor &foreground); //设置当前前景色 void setCurrentBackground(const QColor ¤tBackground); //设置当前前景色 void setCurrentForeground(const QColor ¤tForeground); }; #endif // NAVPROGRESS_H
void NavProgress::paintEvent(QPaintEvent *) { //绘制准备工做,启用反锯齿 QPainter painter(this); painter.setRenderHints(QPainter::Antialiasing | QPainter::TextAntialiasing); //根据不同的样式绘制 if (navStyle == NavStyle_JD) { //绘制背景 drawBg_JD(&painter); //绘制文字 drawText_JD(&painter); //绘制当前背景 drawCurrentBg_JD(&painter); //绘制当前文字 drawCurrentText_JD(&painter); } else if (navStyle == NavStyle_TB) { //绘制背景 drawBg_TB(&painter); //绘制文字 drawText_TB(&painter); //绘制当前背景 drawCurrentBg_TB(&painter); } else if (navStyle == NavStyle_ZFB) { //绘制背景 drawBg_ZFB(&painter); //绘制文字 drawText_ZFB(&painter); //绘制当前背景 drawCurrentBg_ZFB(&painter); } } void NavProgress::drawBg_JD(QPainter *painter) { painter->save(); //圆半径为高度必定比例,计算宽度,将宽度等分 int width = this->width() / maxStep; int height = this->height() / 2; int radius = height / 2; int initX = 0; int initY = height / 2 + radius / 5; //逐个绘制链接线条 initX = width / 2; QPen pen; pen.setWidthF((double)radius / 4); pen.setCapStyle(Qt::RoundCap); pen.setColor(background); painter->setPen(pen); painter->setBrush(Qt::NoBrush); for (int i = 0; i < maxStep - 1; i++) { painter->drawLine(QPoint(initX, initY), QPoint(initX + width, initY)); initX += width; } //逐个绘制圆 initX = width / 2; painter->setPen(Qt::NoPen); painter->setBrush(background); for (int i = 0; i < maxStep; i++) { painter->drawEllipse(QPoint(initX, initY), radius, radius); initX += width; } //逐个绘制圆中的数字 initX = width / 2; QFont font; font.setPixelSize(radius); painter->setFont(font); painter->setPen(foreground); painter->setBrush(Qt::NoBrush); for (int i = 0; i < maxStep; i++) { QRect textRect(initX - radius, initY - radius, radius * 2, radius * 2); painter->drawText(textRect, Qt::AlignCenter, QString::number(i + 1)); initX += width; } painter->restore(); } void NavProgress::drawText_JD(QPainter *painter) { int width = this->width() / maxStep; int height = this->height() / 2; int initX = 0; int initY = height; painter->save(); QFont font; font.setPixelSize(height / 3); painter->setFont(font); painter->setPen(background); painter->setBrush(Qt::NoBrush); for (int i = 0; i < maxStep; i++) { QRect textRect(initX, initY, width, height); painter->drawText(textRect, Qt::AlignCenter, topInfo.at(i)); initX += width; } painter->restore(); }