├── .gitattributes ├── .gitignore ├── CustomControls.pro ├── CustomLineEdit ├── CustomLineEdit.cpp ├── CustomLineEdit.h ├── LineEditpart.cpp └── LineEditpart.h ├── LEDNumber ├── LedNumber.cpp └── LedNumber.h ├── Loading ├── Loading.cpp └── Loading.h ├── README.md ├── RoundDot ├── RoundDot.cpp └── RoundDot.h ├── RoundProgressBar ├── roundprogressbar.cpp └── roundprogressbar.h ├── SelfAdaptableLabel ├── SelfAdaptableLabel.cpp └── SelfAdaptableLabel.h ├── main.cpp ├── mainwindow.cpp ├── mainwindow.h └── mainwindow.ui /.gitattributes: -------------------------------------------------------------------------------- 1 | *.js linguist-language=python 2 | *.css linguist-language=python 3 | *.html linguist-language=python -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | *.user -------------------------------------------------------------------------------- /CustomControls.pro: -------------------------------------------------------------------------------- 1 | #------------------------------------------------- 2 | # 3 | # Project created by QtCreator 2018-11-25T10:48:22 4 | # 5 | #------------------------------------------------- 6 | 7 | QT += core gui 8 | 9 | greaterThan(QT_MAJOR_VERSION, 4): QT += widgets 10 | 11 | TARGET = LEDNumber 12 | TEMPLATE = app 13 | 14 | # The following define makes your compiler emit warnings if you use 15 | # any feature of Qt which has been marked as deprecated (the exact warnings 16 | # depend on your compiler). Please consult the documentation of the 17 | # deprecated API in order to know how to port your code away from it. 18 | DEFINES += QT_DEPRECATED_WARNINGS 19 | 20 | # You can also make your code fail to compile if you use deprecated APIs. 21 | # In order to do so, uncomment the following line. 22 | # You can also select to disable deprecated APIs only up to a certain version of Qt. 23 | #DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0 24 | 25 | 26 | SOURCES += \ 27 | main.cpp \ 28 | mainwindow.cpp \ 29 | LEDNumber/LedNumber.cpp \ 30 | Loading/Loading.cpp \ 31 | RoundDot/RoundDot.cpp \ 32 | RoundProgressBar/roundprogressbar.cpp \ 33 | SelfAdaptableLabel/SelfAdaptableLabel.cpp 34 | 35 | HEADERS += \ 36 | mainwindow.h \ 37 | CustomLineEdit/CustomLineEdit.h \ 38 | CustomLineEdit/LineEditpart.h \ 39 | LEDNumber/LedNumber.h \ 40 | Loading/Loading.h \ 41 | RoundDot/RoundDot.h \ 42 | RoundProgressBar/roundprogressbar.h \ 43 | SelfAdaptableLabel/SelfAdaptableLabel.h 44 | 45 | FORMS += \ 46 | mainwindow.ui 47 | -------------------------------------------------------------------------------- /CustomLineEdit/CustomLineEdit.cpp: -------------------------------------------------------------------------------- 1 | #include "CustomLineEdit.h" 2 | #include 3 | 4 | CustomLineEdit::CustomLineEdit(QWidget *parent) : QWidget(parent) 5 | { 6 | //默认输入为6个 7 | setInputCount(6); 8 | //默认开启密码样式 9 | setPassWordMode(true); 10 | //默认显示装饰线 11 | showVerticalLine(true); 12 | 13 | QHBoxLayout *layout=new QHBoxLayout(this); 14 | layout->setMargin(14); 15 | layout->setSpacing(2); 16 | this->setLayout(layout); 17 | for(int i=0;i<_inputCount;i++) 18 | { 19 | LineEditPart* part=new LineEditPart(this); 20 | //当文字内容变化时,获取内容信息,并改变聚焦 21 | connect(part,&LineEditPart::textChanged,[=]{ 22 | textList[i]=part->text().simplified(); 23 | if(textList[i].isEmpty()) 24 | { 25 | if(i!=0) 26 | partList.at(i-1)->setFocus(); 27 | } 28 | else if(i<_inputCount-1) 29 | partList.at(i+1)->setFocus(); 30 | emit textChanged(); 31 | }); 32 | connect(part,&LineEditPart::leftAndRightPressed,[=](int a){ 33 | if(a) 34 | { 35 | qDebug()<<"right"; 36 | LineEditPart* part=(LineEditPart*)(this->focusWidget()); 37 | int index=partList.indexOf(part); 38 | if(indexsetFocus(); 40 | } 41 | else 42 | { 43 | qDebug()<<"left"; 44 | LineEditPart* part=(LineEditPart*)(this->focusWidget()); 45 | int index=partList.indexOf(part); 46 | if(index) 47 | partList.at(index-1)->setFocus(); 48 | } 49 | }); 50 | part->setEchoMode(QLineEdit::Password); 51 | layout->addWidget(part); 52 | partList.append(part); 53 | textList.append(QString("")); 54 | 55 | //最后一个回车时发送回车信号 56 | if(i==_inputCount-1) 57 | connect(part,&LineEditPart::returnPressed,[=]{ 58 | emit returnPressed(); 59 | }); 60 | //加入装饰线 61 | else if(_verticalLine) 62 | { 63 | QFrame* line = new QFrame(this); 64 | line->setObjectName(QStringLiteral("line")); 65 | line->setStyleSheet(QStringLiteral("background-color: rgba(220,220,220,200);border-width:0px;")); 66 | line->setMinimumWidth(1); 67 | //line->setFrameShape(QFrame::VLine); 68 | //line->setFrameShadow(QFrame::Sunken); 69 | layout->addWidget(line); 70 | } 71 | } 72 | } 73 | 74 | void CustomLineEdit::paintEvent(QPaintEvent* event) 75 | { 76 | QStyleOption opt; 77 | opt.init(this); 78 | QPainter p(this); 79 | style()->drawPrimitive(QStyle::PE_Widget, &opt, &p, this); 80 | QWidget::paintEvent(event); 81 | } 82 | 83 | void CustomLineEdit::setInputCount(int count) 84 | { 85 | _inputCount=count; 86 | } 87 | 88 | void CustomLineEdit::setPassWordMode(bool flag) 89 | { 90 | _passwordMode=flag; 91 | } 92 | 93 | void CustomLineEdit::showVerticalLine(bool flag) 94 | { 95 | _verticalLine=flag; 96 | } 97 | 98 | QString CustomLineEdit::text() 99 | { 100 | QString str(""); 101 | for(QString i:textList) 102 | str+=i; 103 | return str; 104 | } 105 | 106 | 107 | 108 | 109 | 110 | 111 | -------------------------------------------------------------------------------- /CustomLineEdit/CustomLineEdit.h: -------------------------------------------------------------------------------- 1 | #ifndef CUSTOMLINEEDIT_H 2 | #define CUSTOMLINEEDIT_H 3 | 4 | #include "LineEditpart.h" 5 | 6 | #include 7 | #include 8 | #include 9 | #include 10 | #include 11 | #include 12 | 13 | class CustomLineEdit : public QWidget 14 | { 15 | Q_OBJECT 16 | public: 17 | explicit CustomLineEdit(QWidget *parent = nullptr); 18 | //设置几个输入 19 | void setInputCount(int); 20 | //设置是否需要密码样式 21 | void setPassWordMode(bool); 22 | //获取内容 23 | QString text(); 24 | //显示装饰线 25 | void showVerticalLine(bool); 26 | private: 27 | int _inputCount; 28 | bool _passwordMode; 29 | bool _verticalLine; 30 | QVector partList; 31 | QList textList; 32 | 33 | signals: 34 | void returnPressed(); 35 | void textChanged(); 36 | 37 | public slots: 38 | 39 | protected: 40 | void paintEvent(QPaintEvent *event); 41 | }; 42 | 43 | #endif // CUSTOMLINEEDIT_H 44 | -------------------------------------------------------------------------------- /CustomLineEdit/LineEditpart.cpp: -------------------------------------------------------------------------------- 1 | #include "LineEditpart.h" 2 | #include 3 | 4 | LineEditPart::LineEditPart(QWidget *parent) : QLineEdit(parent) 5 | { 6 | this->setStyleSheet("background-color:rgba(0,0,0,0);border-width:0px;color:rgb(255,255,255);"); 7 | 8 | //设置只能输入一个字符 9 | QValidator* validator=new QRegExpValidator(QRegExp("^.{1}$"),this); 10 | this->setValidator(validator); 11 | this->setAlignment(Qt::AlignCenter); 12 | } 13 | 14 | //按键事件 15 | void LineEditPart::keyPressEvent(QKeyEvent *event) 16 | { 17 | switch (event->key()) 18 | { 19 | case Qt::Key_Left: 20 | emit leftAndRightPressed(0); 21 | break; 22 | case Qt::Key_Right: 23 | emit leftAndRightPressed(1); 24 | break; 25 | case Qt::Key_Up: 26 | emit leftAndRightPressed(0); 27 | break; 28 | case Qt::Key_Down: 29 | emit leftAndRightPressed(1); 30 | break; 31 | default: 32 | break; 33 | } 34 | QLineEdit::keyPressEvent(event); 35 | return; 36 | } 37 | 38 | 39 | -------------------------------------------------------------------------------- /CustomLineEdit/LineEditpart.h: -------------------------------------------------------------------------------- 1 | #ifndef LINEEDITPART_H 2 | #define LINEEDITPART_H 3 | 4 | #include 5 | #include 6 | 7 | class LineEditPart : public QLineEdit 8 | { 9 | Q_OBJECT 10 | public: 11 | explicit LineEditPart(QWidget *parent = nullptr); 12 | 13 | signals: 14 | //0 for left 1 for right 15 | void leftAndRightPressed(int); 16 | public slots: 17 | protected: 18 | void keyPressEvent(QKeyEvent *event) override; 19 | }; 20 | 21 | #endif // LINEEDITPART_H 22 | -------------------------------------------------------------------------------- /LEDNumber/LedNumber.cpp: -------------------------------------------------------------------------------- 1 | #include "LedNumber.h" 2 | #include 3 | 4 | LEDNumber::LEDNumber(QWidget *parent) : QWidget(parent) 5 | { 6 | //设置默认宽高比 7 | setScale((float)0.6); 8 | //设置默认背景色 9 | setBackColor(QColor(85,85,85)); 10 | //设置默认文字颜色 11 | setFrontColor(QColor(255,255,255)); 12 | //设置中间线颜色 13 | setLineColor(QColor(60,60,60)); 14 | //设置默认文字 15 | setText("2"); 16 | //设置默认文字大小 17 | setFontSize(40); 18 | //设置默认最小尺寸 19 | this->setMinimumSize(100,100); 20 | } 21 | 22 | //********************************************** 设置部分 **************************************** 23 | //设置文字颜色 24 | void LEDNumber::setFrontColor(const QColor & color) 25 | { 26 | _frontColor=color; 27 | } 28 | 29 | //设置背景色 30 | void LEDNumber::setBackColor(const QColor & color) 31 | { 32 | _backColor=color; 33 | } 34 | 35 | //设置中间线颜色 36 | void LEDNumber::setLineColor(const QColor& color) 37 | { 38 | _lineColor=color; 39 | } 40 | 41 | //设置宽高比 42 | void LEDNumber::setScale(float scale) 43 | { 44 | _scale=scale; 45 | } 46 | 47 | //设置文字 48 | void LEDNumber::setText(QString text) 49 | { 50 | _text=text; 51 | } 52 | 53 | void LEDNumber::setFontSize(int size) 54 | { 55 | _fontSize=size; 56 | } 57 | 58 | //********************************************** 绘制部分 **************************************** 59 | void LEDNumber::resizeEvent(QResizeEvent *event) 60 | { 61 | //计算绘制区域 62 | caculateArea(); 63 | } 64 | 65 | void LEDNumber::paintEvent(QPaintEvent *event) 66 | { 67 | Q_UNUSED(event) 68 | QPainter painter(this); 69 | painter.setRenderHint(QPainter::Antialiasing); 70 | 71 | //计算绘制区域 72 | // caculateArea(); 73 | //绘制背景 74 | drawBack(painter); 75 | //绘制文字 76 | drawText(painter); 77 | //绘制中间线 78 | drawLine(painter); 79 | } 80 | 81 | //计算绘制区域 82 | void LEDNumber::caculateArea() 83 | { 84 | if((this->height()-2)*_scale>(this->width()-2)) 85 | { 86 | _width=this->width()-2; 87 | _height=_width/_scale; 88 | } 89 | else 90 | { 91 | _height=this->height()-2; 92 | _width=_height*_scale; 93 | } 94 | } 95 | 96 | //绘制背景 97 | void LEDNumber::drawBack(QPainter & painter) 98 | { 99 | QPen pen(_backColor,1); 100 | painter.setPen(pen); 101 | QPainterPath path; 102 | path.addRoundedRect(1,1,_width,_height,10,10); 103 | painter.fillPath(path,_backColor); 104 | painter.drawPath(path); 105 | } 106 | 107 | //绘制文字 108 | void LEDNumber::drawText(QPainter& painter) 109 | { 110 | QPen pen(_frontColor); 111 | painter.setPen(pen); 112 | painter.setFont(QFont("Microsoft YaHei",_fontSize,75)); 113 | painter.drawText(0,0,_width,_height,Qt::AlignCenter,_text); 114 | } 115 | 116 | //绘制中间线 117 | void LEDNumber::drawLine(QPainter & painter) 118 | { 119 | QPen pen(_lineColor,3); 120 | painter.setPen(pen); 121 | painter.drawLine(1,_height/2,_width+1,_height/2); 122 | } 123 | -------------------------------------------------------------------------------- /LEDNumber/LedNumber.h: -------------------------------------------------------------------------------- 1 | #ifndef LEDNUMBER_H 2 | #define LEDNUMBER_H 3 | 4 | #include 5 | #include 6 | 7 | class LEDNumber : public QWidget 8 | { 9 | Q_OBJECT 10 | public: 11 | explicit LEDNumber(QWidget *parent = nullptr); 12 | 13 | //设置文字色 14 | void setFrontColor(const QColor&); 15 | //设置背景色 16 | void setBackColor(const QColor&); 17 | //设置中间线颜色 18 | void setLineColor(const QColor&); 19 | //设置宽高比例 20 | void setScale(float); 21 | //设置文字 22 | void setText(QString); 23 | //设置文字大小 24 | void setFontSize(int); 25 | 26 | private: 27 | //文字色 28 | QColor _frontColor; 29 | //背景色 30 | QColor _backColor; 31 | //中间线颜色 32 | QColor _lineColor; 33 | //文字 34 | QString _text; 35 | //绘制矩形宽度 36 | float _width; 37 | float _height; 38 | //长宽比 39 | float _scale; 40 | //文字大小 41 | int _fontSize; 42 | 43 | //计算绘制区域 44 | void caculateArea(); 45 | //绘制背景色 46 | void drawBack(QPainter &); 47 | //绘制文字 48 | void drawText(QPainter &); 49 | //绘制中间线 50 | void drawLine(QPainter &); 51 | 52 | protected: 53 | void paintEvent(QPaintEvent *event); 54 | void resizeEvent(QResizeEvent *event); 55 | 56 | signals: 57 | 58 | public slots: 59 | }; 60 | 61 | #endif // LEDNUMBER_H 62 | -------------------------------------------------------------------------------- /Loading/Loading.cpp: -------------------------------------------------------------------------------- 1 | #include "Loading.h" 2 | #include "qmath.h" 3 | #include 4 | 5 | Loading::Loading(QWidget *parent) : QWidget(parent),_i(0),_interval(50),_index(0) 6 | { 7 | //设置背景透明 8 | //this->setWindowFlags(Qt::FramelessWindowHint | Qt::WindowSystemMenuHint | Qt::WindowMinMaxButtonsHint); 9 | //this->setAttribute(Qt::WA_TranslucentBackground, true); 10 | 11 | setDotColor(QColor(49, 177, 190)); 12 | setDotCount(20); 13 | connect(&timer,&QTimer::timeout,this,&Loading::refresh); 14 | setMaxDiameter(30); 15 | setMinDiameter(5); 16 | } 17 | 18 | //********************************************** 设置部分 ************************************* 19 | //设置点的个数 20 | void Loading::setDotCount(int count) 21 | { 22 | _count=count; 23 | } 24 | 25 | //设置点的颜色 26 | void Loading::setDotColor(const QColor & color) 27 | { 28 | _dotColor=color; 29 | } 30 | 31 | //开始动画 32 | void Loading::start() 33 | { 34 | timer.setInterval(_interval); 35 | timer.start(); 36 | } 37 | 38 | //设置最大直径 39 | void Loading::setMaxDiameter(float max) 40 | { 41 | _maxDiameter=max; 42 | } 43 | 44 | //设置最小直径 45 | void Loading::setMinDiameter(float min) 46 | { 47 | _minDiameter=min; 48 | } 49 | //********************************************** 绘制部分 ************************************* 50 | //刷新界面 51 | void Loading::refresh() 52 | { 53 | repaint(); 54 | } 55 | 56 | void Loading::resizeEvent(QResizeEvent *event) 57 | { 58 | Q_UNUSED(event) 59 | caculate(); 60 | } 61 | 62 | void Loading::paintEvent(QPaintEvent *event) 63 | { 64 | Q_UNUSED(event) 65 | QPainter painter(this); 66 | painter.setRenderHint(QPainter::Antialiasing); 67 | 68 | painter.setPen(_dotColor); 69 | painter.setBrush(_dotColor); 70 | 71 | //绘制点 72 | paintDot(painter); 73 | } 74 | 75 | //计算绘制正方形区域 76 | void Loading::caculate() 77 | { 78 | _squareWidth=qMin(this->width(),this->height()); 79 | float half=_squareWidth/2; 80 | _centerDistance=half-_maxDiameter/2-1; 81 | 82 | float gap=(_maxDiameter-_minDiameter)/(_count-1)/2; 83 | float angleGap=(float)360/_count; 84 | 85 | locationList.clear(); 86 | radiiList.clear(); 87 | 88 | for(int i=0;i<_count;i++) 89 | { 90 | radiiList<<_maxDiameter/2-i*gap; 91 | float radian=qDegreesToRadians(-angleGap*i); 92 | locationList.append(Location(half+_centerDistance*qCos(radian),half-_centerDistance*qSin(radian))); 93 | } 94 | } 95 | 96 | //绘制圆点 97 | void Loading::paintDot(QPainter& painter) 98 | { 99 | for(int i=0;i<_count;i++) 100 | { 101 | painter.setPen(_dotColor); 102 | //半径 103 | float radii=radiiList.at((_index+_count-i)%_count); 104 | 105 | //绘制圆点 106 | painter.drawEllipse(QPointF(locationList.at(i).x,locationList.at(i).y),radii,radii); 107 | //绘制正方形 108 | //painter.drawRect(locationList.at(i).x,locationList.at(i).y,radii,radii); 109 | //绘制文字 110 | //QFont font("Microsoft YaHei",radii*1.2,75); 111 | //painter.setFont(font); 112 | //painter.drawText(QPointF(locationList.at(i).x,locationList.at(i).y),u8"霞"); 113 | } 114 | _index++; 115 | } 116 | -------------------------------------------------------------------------------- /Loading/Loading.h: -------------------------------------------------------------------------------- 1 | #ifndef LOADING_H 2 | #define LOADING_H 3 | 4 | #include 5 | #include 6 | #include 7 | #include 8 | 9 | struct Location 10 | { 11 | public: 12 | explicit Location(float _x,float _y){x=_x;y=_y;} 13 | float x; 14 | float y; 15 | }; 16 | 17 | class Loading : public QWidget 18 | { 19 | Q_OBJECT 20 | public: 21 | explicit Loading(QWidget *parent = nullptr); 22 | //设置圆点个数 23 | void setDotCount(int); 24 | //设置点颜色 25 | void setDotColor(const QColor&); 26 | //开始 27 | void start(); 28 | //设置圆点最大直径 29 | void setMaxDiameter(float); 30 | //设置圆点最小直径 31 | void setMinDiameter(float); 32 | 33 | private: 34 | //刷新计数 35 | int _index; 36 | //点的颜色 37 | QColor _dotColor; 38 | //点的个数 39 | int _count; 40 | //圆点最小直径 41 | float _minDiameter; 42 | //圆点最大直径 43 | float _maxDiameter; 44 | //绘制圆点 45 | void paintDot(QPainter &); 46 | //计数 47 | int _i; 48 | //时间间隔 单位:毫秒(ms) 49 | int _interval; 50 | //定时器 51 | QTimer timer; 52 | //绘制区域边长 53 | float _squareWidth; 54 | //圆的直径 55 | float _centerDistance; 56 | //直径列表 57 | QList radiiList; 58 | //圆点坐标列表 59 | QList locationList; 60 | 61 | //计算 62 | void caculate(); 63 | 64 | protected: 65 | void paintEvent(QPaintEvent *event); 66 | void resizeEvent(QResizeEvent *event); 67 | signals: 68 | 69 | public slots: 70 | 71 | private slots: 72 | void refresh(); 73 | }; 74 | 75 | #endif // LOADING_H 76 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # CustomControls-Qt 2 | Qt常见自定义控件比如圆形进度条,loading,LEDNumber等等 3 | -------------------------------------------------------------------------------- /RoundDot/RoundDot.cpp: -------------------------------------------------------------------------------- 1 | #include "RoundDot.h" 2 | 3 | #include 4 | #include "qmath.h" 5 | #include 6 | 7 | RoundDot::RoundDot(QWidget *parent) : QWidget(parent) 8 | { 9 | SetColor(QColor(49,190,169)); 10 | setRatio(0.4); 11 | } 12 | 13 | void RoundDot::SetColor(const QColor&color) 14 | { 15 | _color=color; 16 | } 17 | 18 | void RoundDot::setRatio(float ratio) 19 | { 20 | _ratio=ratio; 21 | } 22 | 23 | void RoundDot::paintEvent(QPaintEvent *event) 24 | { 25 | QPainter painter(this); 26 | painter.setRenderHint(QPainter::Antialiasing); 27 | painter.setPen(_color); 28 | painter.setBrush(_color); 29 | 30 | //半径 31 | float radii=qMin(this->geometry().width(),this->geometry().height())/2; 32 | //绘制圆点 33 | painter.drawEllipse(QPointF(this->geometry().width()/2,this->geometry().height()/2),radii*_ratio,radii*_ratio); 34 | } 35 | -------------------------------------------------------------------------------- /RoundDot/RoundDot.h: -------------------------------------------------------------------------------- 1 | #ifndef ROUNDDOT_H 2 | #define ROUNDDOT_H 3 | 4 | #include 5 | #include 6 | 7 | class RoundDot : public QWidget 8 | { 9 | Q_OBJECT 10 | public: 11 | explicit RoundDot(QWidget *parent = nullptr); 12 | void SetColor(const QColor &); 13 | void setRatio(float); 14 | 15 | private: 16 | QColor _color; 17 | float _ratio; 18 | 19 | protected: 20 | void paintEvent(QPaintEvent* event); 21 | 22 | signals: 23 | 24 | public slots: 25 | }; 26 | 27 | #endif // ROUNDDOT_H 28 | -------------------------------------------------------------------------------- /RoundProgressBar/roundprogressbar.cpp: -------------------------------------------------------------------------------- 1 | #include "roundprogressbar.h" 2 | #include "qmath.h" 3 | #include 4 | #include 5 | 6 | RoundProgressBar::RoundProgressBar(QWidget *parent) : 7 | QWidget(parent),_value(0),_min(0),_max(100),_precision(0) 8 | { 9 | //设置初始角度,顺时针逆时针 10 | setdefault(90,true); 11 | //设置默认外圈宽度 12 | setOutterBarWidth(18); 13 | //设置默认内圈宽度 14 | setInnerBarWidth(16); 15 | //设置默认范围 16 | setRange(0,100); 17 | //设置默认值 18 | setValue(75); 19 | //设置外圈颜色 20 | setOutterColor(QColor(233,248,248)); 21 | //设置默认渐变色 22 | setInnerColor(QColor(49, 177, 190),QColor(133, 243, 244)); 23 | //设置默认文字颜色 24 | setDefaultTextColor(QColor(49,177,190)); 25 | //设置默认精度 26 | setPrecision(0); 27 | //设置内圈默认文字样式 28 | setInnerDefaultTextStyle(RoundProgressBar::percent); 29 | } 30 | 31 | RoundProgressBar::~RoundProgressBar() 32 | { 33 | } 34 | 35 | //********************************************** 外部接口 **************************************** 36 | //设置初始角度,顺时针逆时针 37 | void RoundProgressBar::setdefault(int startAngle,bool clockWise) 38 | { 39 | _startAngle=startAngle; 40 | _clockWise=clockWise; 41 | } 42 | 43 | //设置外圈宽度 44 | void RoundProgressBar::setOutterBarWidth(float width) 45 | { 46 | _outterBarWidth=width; 47 | } 48 | //设置内圈宽度 49 | void RoundProgressBar::setInnerBarWidth(float width) 50 | { 51 | _innerBarWidth=width; 52 | } 53 | 54 | //设置值的范围 55 | void RoundProgressBar::setRange(float min,float max) 56 | { 57 | //todo 和value比较 58 | if(maxsetDuration(500); 75 | animation->setStartValue(_value); 76 | animation->setEndValue(value); 77 | animation->setEasingCurve(QEasingCurve::OutQuad); 78 | animation->start(); 79 | } 80 | 81 | void RoundProgressBar::_setValue(float value) 82 | { 83 | _value=value; 84 | repaint(); 85 | } 86 | 87 | //设置外圈颜色 88 | void RoundProgressBar::setOutterColor(const QColor& outterColor) 89 | { 90 | _outterColor=outterColor; 91 | } 92 | 93 | //设置内圈渐变色 94 | void RoundProgressBar::setInnerColor(const QColor& startColor,const QColor& endColor) 95 | { 96 | _startColor=startColor; 97 | _endColor=endColor; 98 | } 99 | 100 | //设置内圈渐变色 101 | void RoundProgressBar::setInnerColor(const QColor& startColor) 102 | { 103 | _startColor=startColor; 104 | } 105 | 106 | void RoundProgressBar::setDefaultTextColor(const QColor& textColor) 107 | { 108 | _textColor=textColor; 109 | } 110 | 111 | //设置控制 112 | void RoundProgressBar::setControlFlags(int flags) 113 | { 114 | this->_controlFlags|=flags; 115 | } 116 | 117 | //设置显示数字精度 118 | void RoundProgressBar::setPrecision(int precision) 119 | { 120 | _precision=precision; 121 | } 122 | 123 | //********************************************** 内部绘制部分 **************************************** 124 | void RoundProgressBar::resizeEvent(QResizeEvent *event) 125 | { 126 | //根据内外圈宽度设置控件最小大小 127 | if(_outterBarWidth>_innerBarWidth) 128 | this->setMinimumSize(_outterBarWidth*8,_outterBarWidth*8); 129 | else 130 | this->setMinimumSize(_innerBarWidth*8,_innerBarWidth*8); 131 | //计算绘制正方形区域信息 132 | caculateSquare(); 133 | } 134 | 135 | void RoundProgressBar::paintEvent(QPaintEvent *) 136 | { 137 | QPainter painter(this); 138 | painter.setRenderHint(QPainter::Antialiasing); 139 | 140 | //绘制外圈 141 | paintOutterBar(painter); 142 | //绘制内圈 143 | paintInnerBar(painter); 144 | //绘制外圈 145 | paintDot(painter); 146 | //绘制文字 147 | paintText(painter); 148 | } 149 | 150 | //计算绘制正方形区域信息 151 | void RoundProgressBar::caculateSquare() 152 | { 153 | int minWidth=qMin(this->width(),this->height()); 154 | float barWidth=qMax(_outterBarWidth,_innerBarWidth); 155 | _squareWidth=minWidth-barWidth-2; 156 | _squareStart=barWidth/2+1; 157 | _dotX=_squareStart+_squareWidth/2; 158 | _dotY=_squareStart; 159 | } 160 | 161 | //绘制外圈 162 | void RoundProgressBar::paintOutterBar(QPainter &painter) 163 | { 164 | if(!(_controlFlags&outterCirle)) 165 | return; 166 | QPen pen; 167 | pen.setWidth(_outterBarWidth); 168 | pen.setColor(_outterColor); 169 | painter.setPen(pen); 170 | QRectF rectangle(_squareStart,_squareStart,_squareWidth,_squareWidth); 171 | //从90度开始,逆时针旋转 172 | painter.drawEllipse(rectangle); 173 | } 174 | 175 | //绘制内圈 176 | void RoundProgressBar::paintInnerBar(QPainter& painter) 177 | { 178 | QPen pen; 179 | if(!(_controlFlags&linearColor)) 180 | pen.setColor(_startColor); 181 | else 182 | { 183 | QLinearGradient gradient(0, 0, 0, _squareWidth); 184 | gradient.setColorAt(0, _startColor); 185 | gradient.setColorAt(1, _endColor); 186 | QBrush brush(gradient); 187 | pen.setBrush(brush); 188 | } 189 | pen.setWidth(_innerBarWidth); 190 | pen.setStyle(Qt::SolidLine); 191 | pen.setCapStyle(Qt::RoundCap); 192 | pen.setJoinStyle(Qt::RoundJoin); 193 | painter.setPen(pen); 194 | QRectF rectangle(_squareStart,_squareStart,_squareWidth,_squareWidth); 195 | //从90度开始,逆时针旋转 196 | int startAngle=_startAngle*16; 197 | int spanAngle=(_value-_min)/(_max-_min)*360*16*(_clockWise?-1:1); 198 | painter.drawArc(rectangle,startAngle,spanAngle); 199 | } 200 | 201 | //绘制装饰圆点 202 | void RoundProgressBar::paintDot(QPainter& painter) 203 | { 204 | if(!(_controlFlags&decorateDot)) 205 | return; 206 | //当bar宽度小于3时,便不再绘制装饰圆点 207 | if(_innerBarWidth<3) 208 | return; 209 | painter.setPen(QColor(255,255,255)); 210 | painter.setBrush(QColor(255,255,255)); 211 | //区域为圆点绘制正方形区域 212 | painter.drawEllipse(_dotX-_innerBarWidth/6,_dotY-_innerBarWidth/6,_innerBarWidth/3,_innerBarWidth/3); 213 | } 214 | 215 | //绘制默认内置文字 216 | void RoundProgressBar::paintText(QPainter& painter) 217 | { 218 | if(!(_controlFlags&defaultText)) 219 | return; 220 | painter.setPen(_textColor); 221 | painter.setFont(QFont("Microsoft YaHei",22,75)); 222 | switch (_innerDefaultTextStyle) { 223 | case value: 224 | painter.drawText(_squareStart,_squareStart,_squareWidth,_squareWidth,Qt::AlignCenter,QString::number(_value,'f',_precision)); 225 | break; 226 | case valueAndMax: 227 | painter.drawText(_squareStart,_squareStart,_squareWidth,_squareWidth,Qt::AlignCenter, 228 | QString::number(_value,'f',_precision)+"/"+QString::number(_max,'f',_precision)); 229 | break; 230 | case percent: 231 | painter.drawText(_squareStart,_squareStart,_squareWidth,_squareWidth,Qt::AlignCenter, 232 | QString::number(_value/_max*100,'f',_precision)+"%"); 233 | break; 234 | default: 235 | break; 236 | } 237 | } 238 | 239 | 240 | -------------------------------------------------------------------------------- /RoundProgressBar/roundprogressbar.h: -------------------------------------------------------------------------------- 1 | #ifndef ROUNDPROGRESSBAR_H 2 | #define ROUNDPROGRESSBAR_H 3 | 4 | #include 5 | #include 6 | 7 | #define Abs(x) ((x)>=0?(x):-(x)) 8 | 9 | class RoundProgressBar : public QWidget 10 | { 11 | Q_OBJECT 12 | Q_PROPERTY(float _value READ getValue WRITE _setValue) 13 | 14 | public: 15 | explicit RoundProgressBar(QWidget *parent = 0); 16 | ~RoundProgressBar(); 17 | //控制变量 18 | enum SwitchFlags 19 | { 20 | //默认文字 21 | defaultText =0x00000001, 22 | //内圈渐变色 23 | linearColor =0x00000004, 24 | //装饰圆点 25 | decorateDot =0x00000008, 26 | //外圈 27 | outterCirle =0x0000000e, 28 | //动画 29 | animation =0x00000010, 30 | //显示所有效果 31 | all =0xffffffff 32 | }; 33 | //内部文字展示样式 34 | enum InnerDefaultTextStyle 35 | { 36 | //百分比 37 | percent =0x00000001, 38 | //值 39 | value =0x00000002, 40 | //值与最大值 41 | valueAndMax =0x00000004 42 | }; 43 | 44 | //设置初始角度,顺时针逆时针 45 | void setdefault(int,bool); 46 | //设置外圈宽度 47 | void setOutterBarWidth(float); 48 | //设置内圈宽度 49 | void setInnerBarWidth(float); 50 | //设置范围 51 | void setRange(float, float); 52 | //设置当前值 53 | void setValue(float); 54 | //设置外圈颜色 55 | void setOutterColor(const QColor&); 56 | //设置内圈渐变色 57 | void setInnerColor(const QColor&,const QColor&); 58 | void setInnerColor(const QColor&); 59 | //设置默认文字颜色 60 | void setDefaultTextColor(const QColor&); 61 | //设置控制命令 62 | void setControlFlags(int); 63 | //设置显示数字精度 64 | void setPrecision(int); 65 | //设置内圈默认文字样式 66 | inline void setInnerDefaultTextStyle(InnerDefaultTextStyle style){_innerDefaultTextStyle=style;} 67 | 68 | //获取当前值 69 | inline float getValue(){return _value;} 70 | 71 | protected: 72 | void paintEvent(QPaintEvent *); 73 | void resizeEvent(QResizeEvent *event); 74 | 75 | private: 76 | //起始角度 范围0-360 77 | int _startAngle; 78 | //顺时针 79 | bool _clockWise; 80 | //outterBar宽度 81 | float _outterBarWidth; 82 | //innerBar宽度 83 | float _innerBarWidth; 84 | //圆点矩形坐标 85 | float _dotX; 86 | float _dotY; 87 | //最小值,最大值,当前值 88 | float _min; 89 | float _max; 90 | float _value; 91 | //外圈颜色 92 | QColor _outterColor; 93 | //内圈渐变颜色 94 | QColor _startColor; 95 | QColor _endColor; 96 | //默认文字颜色 97 | QColor _textColor; 98 | //小数点精度 99 | int _precision; 100 | //绘制正方形信息 101 | float _squareStart; 102 | float _squareWidth; 103 | //控制信号 104 | quint32 _controlFlags=0x00000000; 105 | //内圈默认文字样式 106 | InnerDefaultTextStyle _innerDefaultTextStyle; 107 | 108 | //绘制外圈 109 | void paintOutterBar(QPainter &); 110 | //绘制内圈 111 | void paintInnerBar(QPainter &); 112 | //绘制装饰圆点 113 | void paintDot(QPainter &); 114 | //绘制默认内置文字 115 | void paintText(QPainter &); 116 | //设置当前值 117 | void _setValue(float); 118 | //计算绘制正方形区域信息 119 | void caculateSquare(); 120 | 121 | }; 122 | 123 | #endif // ROUNDPROGRESSBAR_H 124 | -------------------------------------------------------------------------------- /SelfAdaptableLabel/SelfAdaptableLabel.cpp: -------------------------------------------------------------------------------- 1 | #include "SelfAdaptableLabel.h" 2 | 3 | SelfAdaptableLabel::SelfAdaptableLabel(QWidget *parent) : QLabel(parent) 4 | { 5 | 6 | } 7 | 8 | //void SelfAdaptableLabel::setInitalPixamp(const QPixmap& pixmap) 9 | //{ 10 | // _pixmap=pixmap; 11 | //} 12 | 13 | void SelfAdaptableLabel::resizeEvent(QResizeEvent *event) 14 | { 15 | QPixmap tmp=_pixmap.scaled(this->width(),this->height(),Qt::IgnoreAspectRatio,Qt::FastTransformation); 16 | this->setPixmap(tmp); 17 | } 18 | -------------------------------------------------------------------------------- /SelfAdaptableLabel/SelfAdaptableLabel.h: -------------------------------------------------------------------------------- 1 | #ifndef SELFADAPTABLELABEL_H 2 | #define SELFADAPTABLELABEL_H 3 | 4 | #include 5 | #include 6 | 7 | class SelfAdaptableLabel : public QLabel 8 | { 9 | Q_OBJECT 10 | public: 11 | explicit SelfAdaptableLabel(QWidget *parent = nullptr); 12 | void setInitalPixmap(const QPixmap&); 13 | 14 | private: 15 | QPixmap _pixmap; 16 | 17 | protected: 18 | void resizeEvent(QResizeEvent* event); 19 | }; 20 | 21 | #endif // SELFADAPTABLELABEL_H 22 | -------------------------------------------------------------------------------- /main.cpp: -------------------------------------------------------------------------------- 1 | #include "mainwindow.h" 2 | #include "Loading/Loading.h" 3 | #include 4 | 5 | int main(int argc, char *argv[]) 6 | { 7 | QApplication a(argc, argv); 8 | MainWindow w; 9 | w.show(); 10 | 11 | return a.exec(); 12 | } 13 | -------------------------------------------------------------------------------- /mainwindow.cpp: -------------------------------------------------------------------------------- 1 | #include "mainwindow.h" 2 | #include "ui_mainwindow.h" 3 | 4 | MainWindow::MainWindow(QWidget *parent) : 5 | QMainWindow(parent), 6 | ui(new Ui::MainWindow) 7 | { 8 | ui->setupUi(this); 9 | 10 | //*********************** RoundProgressBar ************************ 11 | bar1=new RoundProgressBar(this); 12 | bar1->setOutterBarWidth(20); 13 | bar1->setInnerBarWidth(20); 14 | bar1->setControlFlags(RoundProgressBar::all); 15 | 16 | bar2=new RoundProgressBar(this); 17 | bar2->setInnerDefaultTextStyle(RoundProgressBar::valueAndMax); 18 | bar2->setOutterBarWidth(8); 19 | bar2->setInnerBarWidth(20); 20 | bar2->setOutterColor(QColor(250,250,240)); 21 | bar2->setDefaultTextColor(QColor(255,190,57)); 22 | bar2->setInnerColor(QColor(255,190,57),QColor(255,230,129)); 23 | bar2->setControlFlags(RoundProgressBar::all); 24 | ui->gridLayout->addWidget(bar1,0,0); 25 | ui->gridLayout->addWidget(bar2,0,1); 26 | 27 | //*********************** LEDNumber ************************ 28 | num1=new LEDNumber(this); 29 | num2=new LEDNumber(this); 30 | num2->setBackColor(QColor(255,85,85)); 31 | num2->setLineColor(QColor(250,250,250)); 32 | num2->setFrontColor(QColor(50,50,50)); 33 | ui->gridLayout_3->addWidget(num1,0,0); 34 | ui->gridLayout_3->addWidget(num2,1,0); 35 | timer.setInterval(1500); 36 | connect(&timer,&QTimer::timeout,this,&MainWindow::setText); 37 | timer.start(); 38 | 39 | //********************** Loading ************************* 40 | loading1=new Loading(this); 41 | loading1->start(); 42 | ui->gridLayout_2->addWidget(loading1,0,0); 43 | 44 | // loading2=new Loading(this); 45 | // loading2->start(); 46 | // ui->gridLayout_2->addWidget(loading2,0,1); 47 | } 48 | 49 | MainWindow::~MainWindow() 50 | { 51 | if(timer.isActive()) 52 | timer.stop(); 53 | delete ui; 54 | } 55 | 56 | void MainWindow::setText() 57 | { 58 | //********************************************* 59 | bar1->setValue(qrand()%100+1); 60 | bar2->setValue(qrand()%100+1); 61 | bar1->repaint(); 62 | bar2->repaint(); 63 | 64 | //********************************************* 65 | num1->setText(QString::number(qrand()%10)); 66 | num2->setText(QString::number(qrand()%10)); 67 | num1->repaint(); 68 | num2->repaint(); 69 | } 70 | 71 | -------------------------------------------------------------------------------- /mainwindow.h: -------------------------------------------------------------------------------- 1 | #ifndef MAINWINDOW_H 2 | #define MAINWINDOW_H 3 | 4 | #include "RoundProgressBar/roundprogressbar.h" 5 | #include "LEDNumber/LedNumber.h" 6 | #include "Loading/Loading.h" 7 | 8 | #include 9 | #include 10 | #include 11 | 12 | namespace Ui { 13 | class MainWindow; 14 | } 15 | 16 | class MainWindow : public QMainWindow 17 | { 18 | Q_OBJECT 19 | 20 | public: 21 | explicit MainWindow(QWidget *parent = 0); 22 | ~MainWindow(); 23 | 24 | private: 25 | Ui::MainWindow *ui; 26 | 27 | //******************************** 28 | RoundProgressBar* bar1; 29 | RoundProgressBar* bar2; 30 | 31 | //******************************** 32 | LEDNumber* num1; 33 | LEDNumber* num2; 34 | QTimer timer; 35 | //******************************** 36 | Loading* loading1; 37 | Loading* loading2; 38 | 39 | private slots: 40 | void setText(); 41 | }; 42 | 43 | #endif // MAINWINDOW_H 44 | -------------------------------------------------------------------------------- /mainwindow.ui: -------------------------------------------------------------------------------- 1 | 2 | 3 | MainWindow 4 | 5 | 6 | 7 | 0 8 | 0 9 | 835 10 | 489 11 | 12 | 13 | 14 | MainWindow 15 | 16 | 17 | 18 | 19 | 20 | 21 | 2 22 | 23 | 24 | 25 | RoundProgressBar 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | LEDNumber 36 | 37 | 38 | 39 | 40 | 41 | Loading 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 0 53 | 0 54 | 835 55 | 26 56 | 57 | 58 | 59 | 60 | 61 | TopToolBarArea 62 | 63 | 64 | false 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | 73 | --------------------------------------------------------------------------------