diff --git a/GroupWidget.cpp b/GroupWidget.cpp new file mode 100644 index 0000000..ba1f1fb --- /dev/null +++ b/GroupWidget.cpp @@ -0,0 +1,16 @@ +#include "GroupWidget.h" + +GroupWidget::GroupWidget(QWidget *parent) + : QDialog(parent) +{ + //设置最小尺寸 + setFixedSize(240,300); + //去掉问号 + setWindowFlags(Qt::CustomizeWindowHint | Qt::WindowCloseButtonHint); + //设置对话框标题 + setWindowTitle(QStringLiteral("加群联系作者")); + setStyleSheet("border-image:url(://Picture/Qt.png)"); +} + +GroupWidget::~GroupWidget() +{} diff --git a/GroupWidget.h b/GroupWidget.h new file mode 100644 index 0000000..8a593dd --- /dev/null +++ b/GroupWidget.h @@ -0,0 +1,15 @@ +#ifndef GROUPWIDGET_H +#define GROUPWIDGET_H + +#include + +class GroupWidget : public QDialog +{ + Q_OBJECT + +public: + GroupWidget(QWidget *parent = 0); + ~GroupWidget(); +}; + +#endif diff --git a/MainWidget.cpp b/MainWidget.cpp new file mode 100644 index 0000000..49322b2 --- /dev/null +++ b/MainWidget.cpp @@ -0,0 +1,77 @@ +#include "MainWidget.h" + +Widget::Widget(QWidget *parent) + : QDialog(parent) +{ + //窗口基础设置 + setFixedSize(300,350); + setWindowFlags(Qt::CustomizeWindowHint | Qt::WindowCloseButtonHint); + setStyleSheet("background-color:rgb(68,68,68);"); + + //初始化控件 + QPushButton* btn_Test=new QPushButton(); + QPushButton* btn_GetCode=new QPushButton(); + QVBoxLayout* mainLayout=new QVBoxLayout(); + + //这只按钮文字 + btn_Test->setText(QStringLiteral("测试")); + btn_GetCode->setText(QStringLiteral("获取源码")); + + //设置按钮高度 + btn_Test->setFixedHeight(30); + btn_GetCode->setFixedHeight(30); + + //设置按钮样式表 + btn_Test->setStyleSheet("border-radius:4px;background-color:rgb(230,230,230);"); + btn_GetCode->setStyleSheet("border-radius:4px;background-color:rgb(230,230,230);"); + + //环形进度条初始化 + round_Bar=new QRoundProgressBar(); + + //环形进度条可选设置 + do{ + //round_Bar->SetStartAngle(-30); + //round_Bar->SetEndAngle(210); + //round_Bar->SetMaxRadius(100); + //round_Bar->SetRingWidth(20); + //round_Bar->SetIcomWidth(25); + //round_Bar->SetCircularBorderColor(qRgb(255,0,0)); + //round_Bar->SetInsideMaskColor(qRgb(0,255,0)); + //round_Bar->SetRadialGradient(qRgb(10,10,10),qRgb(120,0,120)); + //SlotBtnTest(); + }while(0); + + + //初始化并设置布局 + QHBoxLayout* btnLayout=new QHBoxLayout(); + btnLayout->setMargin(10); + btnLayout->setSpacing(10); + btnLayout->addWidget(btn_Test); + btnLayout->addWidget(btn_GetCode); + + mainLayout->setMargin(20); + mainLayout->setSpacing(0); + mainLayout->addSpacing(20); + mainLayout->addWidget(round_Bar); + mainLayout->addLayout(btnLayout); + setLayout(mainLayout); + + //添加信号与槽 + connect(btn_Test,&QPushButton::clicked,this,&Widget::SlotBtnTest); + connect(btn_GetCode,&QPushButton::clicked,this,&Widget::SlotBtnGetCode); +} + +Widget::~Widget() +{ +} + +void Widget::SlotBtnTest() +{ + round_Bar->SetCurrPrograss(qrand()%100); +} + +void Widget::SlotBtnGetCode() +{ + GroupWidget widget; + widget.exec(); +} diff --git a/MainWidget.h b/MainWidget.h new file mode 100644 index 0000000..6e87b98 --- /dev/null +++ b/MainWidget.h @@ -0,0 +1,25 @@ +#ifndef WIDGET_H +#define WIDGET_H + +#include +#include +#include +#include + +#include "GroupWidget.h" +#include "QRoundProgressBar.h" + +class Widget : public QDialog +{ + Q_OBJECT + + QRoundProgressBar* round_Bar; + +public: + Widget(QWidget *parent = nullptr); + ~Widget(); + + void SlotBtnTest(); + void SlotBtnGetCode(); +}; +#endif // WIDGET_H diff --git a/Picture/Qt.png b/Picture/Qt.png new file mode 100644 index 0000000..88ffa68 Binary files /dev/null and b/Picture/Qt.png differ diff --git a/QRoundProgressBar.cpp b/QRoundProgressBar.cpp new file mode 100644 index 0000000..5ed1c99 --- /dev/null +++ b/QRoundProgressBar.cpp @@ -0,0 +1,174 @@ +#include "QRoundProgressBar.h" + +QRoundProgressBar::QRoundProgressBar(QWidget *parent) : + QWidget(parent) +{ + //给变量设置初始值-默认值 + startAngle=-45; + endAngle=225; + ringWidth=20; + maxRadius=110; + iconWidth=15; + currAngle=270; + circularBorderColor=qRgb(120,120,120); + insideMaskColor=qRgb(68,68,68); + startGradientColor=qRgb(80,80,80); + endGradientColor=qRgb(150,140,20); +} + +void QRoundProgressBar::SetStartAngle(int value) +{ + startAngle=value; +} + +void QRoundProgressBar::SetEndAngle(int value) +{ + endAngle=value; +} + +void QRoundProgressBar::SetMaxRadius(int value) +{ + maxRadius=value; +} + +void QRoundProgressBar::SetRingWidth(int value) +{ + ringWidth=value; +} + +void QRoundProgressBar::SetIcomWidth(int value) +{ + iconWidth=value; +} + +void QRoundProgressBar::SetCircularBorderColor(QRgb value) +{ + circularBorderColor=value; +} + +void QRoundProgressBar::SetInsideMaskColor(QRgb value) +{ + insideMaskColor=value; +} + +void QRoundProgressBar::SetRadialGradient(QRgb startColor,QRgb endColor) +{ + startGradientColor=startColor; + endGradientColor=endColor; +} + +void QRoundProgressBar::SetCurrPrograss(int value) +{ + currAngle=value; + update(); +} + +void QRoundProgressBar::paintEvent(QPaintEvent*) +{ + //初始化变量 + QPainter painter(this); + // 右移1位 相当于width()/2 + painter.translate(width() >> 1, height() >> 1); + //启动反锯齿 + painter.setRenderHint(QPainter::Antialiasing); + + //绘制外圆 + DrawOutSideCircle(painter); + //绘制两个小圆外圆 + DrawTwoSmallCircle(painter,0); + //绘制圆环 + DrawCircularRing(painter,0); + //绘制两个小圆内圆 + DrawTwoSmallCircle(painter,1); + //绘制渐变色圆环 + DrawGradientCircle(painter,maxRadius-1); + //绘制内圆 + DrawInSideCircle(painter); + //绘制进度小圆 + DrawBarIcon(painter); +} + +void QRoundProgressBar::GradientArc(QPainter &painter,int radius,QRgb color) +{ + // 渐变色 + QRadialGradient gradient(0, 0, radius); + gradient.setColorAt(0, color); + gradient.setColorAt(1.0, color); + painter.setBrush(gradient); + + QRectF rect(-radius, -radius, radius << 1, radius << 1); + QPainterPath path; + path.arcTo(rect, startAngle, endAngle-startAngle); + + painter.setPen(Qt::NoPen); + painter.drawPath(path); +} + +void QRoundProgressBar::GradientFullArc(QPainter &painter,QPointF& point,int radius,QRgb color) +{ + // 渐变色 + QRadialGradient gradient(0, 0, radius); + gradient.setColorAt(0, color); + gradient.setColorAt(1.0, color); + painter.setBrush(gradient); + painter.drawEllipse(point,radius,radius); +} + +void QRoundProgressBar::DrawOutSideCircle(QPainter& painter) +{ + GradientArc(painter,maxRadius,circularBorderColor); +} + +void QRoundProgressBar::DrawCircularRing(QPainter& painter,int) +{ + GradientArc(painter,maxRadius-1,startGradientColor); +} + +void QRoundProgressBar::DrawTwoSmallCircle(QPainter& painter,int type) +{ + //计算小圆坐标 + QPoint rightCircle(0,0); + QPoint leftCircle(0,0); + rightCircle.setY(-(qSin(startAngle*M_PI/180)*(maxRadius-ringWidth/2))); + rightCircle.setX(qCos(startAngle*M_PI/180)*(maxRadius-ringWidth/2)+1); + leftCircle.setX(-rightCircle.rx()); + leftCircle.setY(rightCircle.ry()); + if(type==0){ + painter.drawEllipse(rightCircle,ringWidth/2,ringWidth/2); + painter.drawEllipse(leftCircle,ringWidth/2,ringWidth/2); + } + else{ + painter.drawEllipse(rightCircle,ringWidth/2-1,ringWidth/2-1); + painter.drawEllipse(leftCircle,ringWidth/2-1,ringWidth/2-1); + } +} + +void QRoundProgressBar::DrawInSideCircle(QPainter& painter) +{ + QPointF point(0,0); + GradientArc(painter,maxRadius-ringWidth+1,circularBorderColor); + GradientFullArc(painter,point,maxRadius-ringWidth,insideMaskColor); + //GradientFullArc(painter,maxRadius*3/4,qRgb(48,48,48)); +} + +void QRoundProgressBar::DrawGradientCircle(QPainter& painter,int radius) +{ + QConicalGradient conicalGradient(QPointF(0, 0), endAngle+1); + conicalGradient.setColorAt((360-qAbs(endAngle-startAngle)+currAngle)/360.0, endGradientColor); + conicalGradient.setColorAt(1, startGradientColor); + painter.setBrush(conicalGradient); + QPainterPath path; + QRectF rect(-radius, -radius, radius << 1, radius << 1); + path.arcTo(rect, startAngle+currAngle, endAngle-startAngle-currAngle); + painter.setPen(Qt::NoPen); + painter.drawPath(path); +} + +void QRoundProgressBar::DrawBarIcon(QPainter& painter) +{ + QPointF point(0,0); + point.setX(point.x()+qCos((startAngle+currAngle)*M_PI/180)*(maxRadius-ringWidth/2)); + point.setY(point.y()-qSin((startAngle+currAngle)*M_PI/180)*(maxRadius-ringWidth/2)); + GradientFullArc(painter,point,iconWidth,endGradientColor); + GradientFullArc(painter,point,iconWidth*0.6,startGradientColor); +} diff --git a/QRoundProgressBar.h b/QRoundProgressBar.h new file mode 100644 index 0000000..2f6b6e6 --- /dev/null +++ b/QRoundProgressBar.h @@ -0,0 +1,61 @@ +#ifndef QROUNDPROGRESSBAR_H +#define QROUNDPROGRESSBAR_H + +#include +#include +#include +#include +#include +#include +#include +#include + +class QRoundProgressBar : public QWidget +{ + Q_OBJECT + +private: + int startAngle; //起始角度 + int endAngle; //结束角度 + int ringWidth; //环宽度 + int maxRadius; //大圆半径 + int iconWidth; //图标的宽度 + int currAngle; //当前进度条对应的角度 + + QRgb circularBorderColor; + QRgb insideMaskColor; + QRgb startGradientColor; + QRgb endGradientColor; + +private: + void DrawBarIcon(QPainter&); + void GradientArc(QPainter&,int,QRgb); + void GradientFullArc(QPainter&,QPointF&,int,QRgb); + void DrawOutSideCircle(QPainter&); + void DrawCircularRing(QPainter&,int); + void DrawTwoSmallCircle(QPainter&,int); + void DrawInSideCircle(QPainter&); + void DrawGradientCircle(QPainter&,int); + +public: + explicit QRoundProgressBar(QWidget *parent = nullptr); + +protected: + void paintEvent(QPaintEvent *event); + +public: + //初始化调用函数 + void SetStartAngle(int); //设置起始角度值 + void SetEndAngle(int); //设置结束角度值 + void SetMaxRadius(int); //设置圆环外圆半径 + void SetRingWidth(int); //设置圆环宽度 + void SetIcomWidth(int); //设置进度图标大小 + void SetCircularBorderColor(QRgb); //设置圆环边框颜色 + void SetInsideMaskColor(QRgb); //设置内圆遮罩颜色 + void SetRadialGradient(QRgb,QRgb); //设置圆环渐变色 + + void SetCurrPrograss(int); //设置当前进度条进度值 + +}; + +#endif diff --git a/QRoundProgressBar.pro b/QRoundProgressBar.pro new file mode 100644 index 0000000..b850304 --- /dev/null +++ b/QRoundProgressBar.pro @@ -0,0 +1,35 @@ +QT += core gui + +greaterThan(QT_MAJOR_VERSION, 4): QT += widgets + +CONFIG += c++11 + +# The following define makes your compiler emit warnings if you use +# any Qt feature that has been marked deprecated (the exact warnings +# depend on your compiler). Please consult the documentation of the +# deprecated API in order to know how to port your code away from it. +DEFINES += QT_DEPRECATED_WARNINGS + +# You can also make your code fail to compile if it uses deprecated APIs. +# In order to do so, uncomment the following line. +# You can also select to disable deprecated APIs only up to a certain version of Qt. +#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0 + +SOURCES += \ + GroupWidget.cpp \ + QRoundProgressBar.cpp \ + main.cpp \ + MainWidget.cpp + +HEADERS += \ + GroupWidget.h \ + MainWidget.h \ + QRoundProgressBar.h + +# Default rules for deployment. +qnx: target.path = /tmp/$${TARGET}/bin +else: unix:!android: target.path = /opt/$${TARGET}/bin +!isEmpty(target.path): INSTALLS += target + +RESOURCES += \ + pic.qrc diff --git a/main.cpp b/main.cpp new file mode 100644 index 0000000..2cd24cd --- /dev/null +++ b/main.cpp @@ -0,0 +1,11 @@ +#include "MainWidget.h" + +#include + +int main(int argc, char *argv[]) +{ + QApplication a(argc, argv); + Widget w; + w.show(); + return a.exec(); +} diff --git a/pic.qrc b/pic.qrc new file mode 100644 index 0000000..84742a0 --- /dev/null +++ b/pic.qrc @@ -0,0 +1,5 @@ + + + Picture/Qt.png + +