.h
virtual void paintEvent(QPaintEvent *event) override;
void updent();
QTimer time;
int startrecv = 0;
.cpp
Widget::Widget(QWisget *parent)
{
ui->setupUi(this);
connect(&time,&QTimer::timeout,this,&Widget::updent);
time.start(50);
}
void Widget::updent()
{
startrecv += 5;
if(startrecv > 360)
{
startrecv = 0;
}
update(); //刷新界面
}
void Widget::paintEvent(QPaintEvent *event)
{
QPainter painter(this);
painter.setRenderHint(QPainter::Antialiasing,true); //设置抗锯齿
painter.setBrush(Qt::black); //设置画刷
painter.drawRect( this->rect() ); //绘制矩形,将界面全部涂黑
painter.translate( rect().center() ); //将坐标系转移到屏幕中心
painter.setPen( QPen(Qt::green,4) ); //设置画笔
int side = qMin( width(),height() )/2/7 //qMin用于计算两个数之间的最小值,side表示最小圆圈的半径
painter.setBrush( Qt::NoBrush ); //丢掉画刷
//绘制圆形********************************************************
for(int i = 1; i <= 7; i++)
{
painter.drawEllipse( QPoint(0,0) , side*i , side*i ); //绘制圆形
}
painter.drawLine( -side*7,0 , side*7,0 ); //绘制x轴
painter.drawLine(0,-side*7 , 0,side*7); //绘制y轴
//****************************************************************
//锥形渐变 起始角度为 startrecv *********************************
QConicalGradient co( 0,0, -startrecv );
co.setColorAt(0,QColor(0,255,0,200));
co.setColorAt(0.1,QColor(0,255,0,100));
co.setColorAt(0.2,QColor(0,255,0,0));
co.setColorAt(1,QColor(0,255,0,0));
painter.setPen(Qt::NoPen);
painter.setBrush(co);
//****************************************************************
//画扇形**********************************************************
int x = -qMin( width(),height() )/2;
int y = -qMin( width(),height() )/2;
int w = qMin( width(),height() );
int h = qMin( width(),height() );
QRectF pieRect(x,y,w,h);
painter.drawPie(pieRect,-startrecv*16,36*16)//画扇形
}