Qt5-雷达项目
界面:
widget.h
#ifndef WIDGET_H
#define WIDGET_H
#include <QTimer>
#include <QWidget>
QT_BEGIN_NAMESPACE
namespace Ui { class Widget; }
QT_END_NAMESPACE
class Widget : public QWidget
{
Q_OBJECT
public:
Widget(QWidget *parent = nullptr);
~Widget();
protected:
void paintEvent(QPaintEvent *event) override;
private:
Ui::Widget *ui;
QTimer *timer;
int startAngle=0;
};
#endif // WIDGET_H
widget.c
#include "widget.h"
#include "ui_widget.h"
#include <QPainter>
#include <QTimer>
Widget::Widget(QWidget *parent)
: QWidget(parent)
, ui(new Ui::Widget)
{
ui->setupUi(this);
timer = new QTimer(this);
connect(timer, &QTimer::timeout,[=](){
//不断变化扇形的启动位置
startAngle += 4;
if(startAngle >= 360){
startAngle = 0;
}
update();
});
timer->start(30);
}
Widget::~Widget()
{
delete ui;
}
void Widget::paintEvent(QPaintEvent *event)
{
QPainter paint(this);//实列化一个画家
paint.setRenderHint(QPainter::Antialiasing,true);//时画出来的看起来更顺滑
QBrush brush(Qt::gray);//实列化一个画刷
paint.setBrush(brush);//将画刷交给画家paint
//应为现在画家拿的是黑色画刷所有画出来黑色的矩形,rect为窗口这个矩形
paint.drawRect(rect());
paint.setBrush(Qt::NoBrush);//将画家的画刷丢掉
QPen pen(Qt::green,3);//实力画一个粗细程度为3的绿色画笔
paint.translate(rect().center());//设置0点
paint.setPen(pen);//将画笔交给画家,现在画家拿的是画笔
/*画圆*/
paint.drawEllipse(QPoint(0,0),1,1);//在(0,0)画一个100圆
paint.drawEllipse(QPoint(0,0),50,50);//在(0,0)画一个100圆
paint.drawEllipse(QPoint(0,0),100,100);//在(0,0)画一个100圆
paint.drawEllipse(QPoint(0,0),150,150);//在(0,0)画一个100圆
paint.drawEllipse(QPoint(0,0),200,200);//在(0,0)画一个100圆
paint.drawEllipse(QPoint(0,0),250,250);//在(0,0)画一个100圆
/*画横线*/
paint.drawLine(0,-250,0,250);
paint.drawLine(-250,0,250,0);
/*锥形渐变*/
QConicalGradient conicalGradient(0, 0, -startAngle);
conicalGradient.setColorAt(0,QColor(0,255,0,200));
conicalGradient.setColorAt(0.1,QColor(0,255,0,100));
conicalGradient.setColorAt(0.2,QColor(0,255,0,0));
conicalGradient.setColorAt(1,QColor(0,255,0,0));
paint.setBrush(conicalGradient);
paint.setPen(Qt::NoPen);
/*画扇形*/
QRect Rect_Pie(-250,-250,500,500);
paint.drawPie(Rect_Pie,-startAngle*16,120*16);
}