1 Star 1 Fork 0

仓葵与暮/os-project-2

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
passrate.cpp 3.92 KB
一键复制 编辑 原始数据 按行查看 历史
仓葵与暮 提交于 2021-09-17 19:54 . 初始化
#include "passrate.h"
#include "ui_passrate.h"
Passrate::Passrate(QWidget *parent) :
QWidget(parent),
ui(new Ui::Passrate)
{
ui->setupUi(this);
lineCount = 100;
value = 100;
bgERadius = 30;
textSize = 12;
outLineRadius = 75;
innLineRadius = 60;
bgERadius = 45;
textOutRadius = 30;
textInnRadius = 20;
}
Passrate::~Passrate()
{
delete ui;
}
void Passrate::paintEvent(QPaintEvent *event){//界面绘制事件
int width = this->width();
int height = this->height();
int side = qMin(width, height);
QPainter painter(this);
painter.setRenderHints(QPainter::Antialiasing | QPainter::TextAntialiasing);//设置渲染,启动反锯齿
painter.translate(width / 2, height / 2);//平移坐标系到窗口正中央
painter.scale(side / 200.0, side / 200.0);//绘制的图片对窗口进行自适应
drawBGE(&painter); //画背景圆
drawTextE(&painter);//画文字所在圆弧
drawText(&painter);//画文字
drawLines(&painter);//画最外部的线
}
void Passrate::updateValue(float value){//提供给外部的方法,用于更新value
this->value = value;
update();//每次更新value后,都重绘界面
}
void Passrate::drawLines(QPainter* painter){
painter->save();
QPen pen;
//pen.setColor(QColor("#A7DD42"));//初始化默认所有的线都应该显示(100%)
pen.setColor(QColor("#a8ff60"));
pen.setWidth(2);
painter->setPen(pen);
float range = 270.0/lineCount; //自定义所有的线加起来占270度,100条线
painter->rotate(135);//旋转135度,开始画线,总共画270度。
QLine line(QPoint(outLineRadius,0),QPoint(innLineRadius,0));//最外圈的小横线,100条,按照坐标从outLineRadius到innLineRadius
for(int i = 1;i<=lineCount;i++){
QPen pen;
pen.setWidth(2);
if(i>60&&i<=value){
// QPen pen;
pen.setColor(QColor("#F1FA02"));
// pen.setWidth(2);
painter->setPen(pen);
}
if(i>90&&i<=value){
// QPen pen;
pen.setColor(QColor("#FF0107"));
// pen.setWidth(2);
painter->setPen(pen);
}
if(i>value){//把应该不显示的部分变成灰色(按照中间的百分比数)
// QPen pen;
pen.setColor(QColor("#D7D7D7"));
// pen.setWidth(2);
painter->setPen(pen);
}
painter->drawLine(line);
painter->rotate(range);
}
painter->restore();
}
void Passrate::drawBGE(QPainter* painter){
painter->save();
painter->setPen(Qt::NoPen);
painter->setBrush(QColor("#EAEAEA"));
QRect rect(-bgERadius,-bgERadius,bgERadius*2,bgERadius*2);
painter->drawEllipse(rect);//画出背景圆(灰色)
painter->restore();
}
void Passrate::drawTextE(QPainter* painter){
painter->save();
painter->setPen(Qt::NoPen);
painter->setBrush(QColor("#2DC877"));//画出中间的绿色圆环
QPainterPath path;
QRect rectOut(-textOutRadius,-textOutRadius,textOutRadius*2,textOutRadius*2);
path.arcTo(rectOut,0,360);//画出圆环外围
QPainterPath subPath;
QRect rectInn(-textInnRadius,-textInnRadius,textInnRadius*2,textInnRadius*2);
subPath.addEllipse(rectInn);//内芯
path -= subPath;
painter->drawPath(path);
painter->restore();
}
void Passrate::drawText(QPainter* painter){
painter->save();
painter->setPen(QColor("#62A0DB"));
QRect rectInn(-textInnRadius,-textInnRadius,textInnRadius*2,textInnRadius*2);//???????
QFont font = painter->font();
font.setPixelSize(textSize);//设置字体大小
font.setFamily("Microsoft YaHei");
painter->setFont(font);
painter->drawText(rectInn,Qt::AlignCenter,QString::number(value)+"%");
painter->restore();
}
void Passrate::delValue()
{
value--;
updateValue(value);
}
void Passrate::addValue()
{
value++;
updateValue(value);
}
int Passrate::getValue() const
{
return value;
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
C++
1
https://gitee.com/fortherepublic-cpp/os-project-2.git
git@gitee.com:fortherepublic-cpp/os-project-2.git
fortherepublic-cpp
os-project-2
os-project-2
master

搜索帮助