代码实现
绘制同心圆,x和y轴实线,45度对角线虚线。
复制代码
void Radar::drawCircle(QPainter *painter)
{
painter->save();
QColor color = QColor(0, 100, 0);
QPen pen = QPen(color, 2);
painter->setPen(pen);
for(int i=0; i<4; i++)
{
int r = this->radius / 4 * (i+1);
painter->drawEllipse(-r, -r, r * 2, r * 2);
}
for(int i=0; i<=4; i++) // x,y 轴线是实线
{
int r = this->radius;
if((i%2) == 0) {
pen.setStyle(Qt::SolidLine);
}
else{
pen.setStyle(Qt::DashDotLine);
//pen.setDashPattern(QVector
}
painter->setPen(pen);
painter->drawLine(-r, 0, r, 0);
painter->rotate(90/2);
}
painter->restore();
}
复制代码
绘制扫描图,依靠锥形渐变颜色,通过透明度控制形成扫描效果,然后绘制饼图。
复制代码
void Radar::drawScan(QPainter *painter)
{
painter->save();
//锥形渐变颜色,通过透明度控制形成扫描效果
QConicalGradient conicalGradient(0, 0, this->angle);
QColor color = QColor(0,150,0);
color.setAlpha(100);
conicalGradient.setColorAt(0, color);
color.setAlpha(20);
conicalGradient.setColorAt(0.1, color);
color.setAlpha(0);
conicalGradient.setColorAt(1, color);
//绘制饼圆
QPen pen;
pen.setWidth(5);
pen.setBrush(conicalGradient);
painter->setPen(pen);
painter->setBrush(conicalGradient);
int r = this->radius;
QRect rect(-r, -r, r * 2, r * 2);
painter->drawPie(rect, this->angle * 16, 360 * 16); // 1/16th of a degree
painter->restore();
}
复制代码
给雷达加上模拟的扫描目标,实现起来很简单,就是打点。
复制代码
void Radar::drawPoints(QPainter *painter)
{
painter->save();
QColor color = QColor(200, 10, 0);
QPen pen = QPen(color, 1);
painter->setPen(pen);
painter->setBrush(color);
for(int i=0; i
{
int r = 6;
QRect rect(points[i].x() - r/2, points[i].y() - r/2, r * 2, r * 2);
painter->drawEllipse(rect);// 画实心小点
}
painter->restore();
}
复制代码
最终效果如下。
外层怎么 调用?使用信号和槽驱动Radar类对象。
复制代码
RadarScan::RadarScan(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::RadarScan)
{
ui->setupUi(this);
// start and stop
connect(this, &RadarScan::radar_state_changed,
ui->radar, &Radar::radar_start_control);
connect(ui->startButton, &QPushButton::clicked, [=](){
emit radar_state_changed(true);
});
connect(ui->stopButton, &QPushButton::clicked, [=](){
emit radar_state_changed(false);
});
// speed control
connect(this, &RadarScan::radar_speed_changed,
ui->radar, &Radar::radar_speed_control);
connect(ui->fastButton, &QPushButton::clicked, [=](){
emit radar_speed_changed(true);
});
connect(ui->slowButton, &QPushButton::clicked, [=](){
emit radar_speed_changed(false);
});
connect(ui->randomButton, &QPushButton::clicked, ui->radar, &Radar::radar_random_point);
}
深圳网站建设www.sz886.com