qcustomplot 绘制 频谱图 瀑布图,游标实现跟随曲线数据的实时展示

qcustomplot 绘制 频谱图 瀑布图,游标实现跟随曲线数据的实时展示

文件结构
在这里插入图片描述
pri文件结构
重写qcustomplot
#ifndef SPECTRUMDISPLAY_H
#define SPECTRUMDISPLAY_H

#include
#include
#include<qpainter.h>
#include
#include<qcustomplot/qcustomplot.h>
#include
#include
class SpectrumDisplay : public QCustomPlot
{
Q_OBJECT
public:
explicit SpectrumDisplay(QWidget *parent = nullptr);
//QCustomPlot *customplot;
QCPItemText *textLabel;
QCPItemTracer *tracer_h;
QCPItemText *textLabel2;
QCPItemRect *rect;//目标迹点矩形追踪
QQueue<QVector> appitlude_fre;
enum direction //创建垂直和水平迹线
{
vertical = 1 ,
horiental = 2,
};
QPointF pos;
QPointF pos2;
void setXaxisx(double xmin,double xmax);//设置X轴范围
void setYaxisy(double ymin,double ymax);//设置y轴范围
void drawCurse(direction);//设置游标
void setPlotStyle();
void setdata(QVector freq,QVectorappitude,QColor color);
void TargetTraceRectangleTracking(QVector,QVector);
void drawDataEnvelop(QVector freq,QVectorappitude,QColor color);
void EnableBackgroundZoom(bool);//启用自由缩放
void EnableOpengl(bool);
virtual void paintEvent(QPaintEvent *);
virtual void mouseMoveEvent(QMouseEvent *event);
signals:

public slots:
};

#endif // SPECTRUMDISPLAY_H
绘制游标
void SpectrumDisplay:: drawCurse(direction dir)//设置游标
{
if(dir == vertical )
{
/绘制游标直线**********/
QCPItemStraightLine line= new QCPItemStraightLine (this);
QPen linesPen(Qt::green, 1, Qt::DashLine);
line->setPen(linesPen);
line->setClipToAxisRect(true);
line->point1->setCoords(0,0);
line->point2->setCoords(0,0);
line->setVisible(false);
/绘制游标箭头
**********/
QCPItemLine arrow = new QCPItemLine(this);
arrow->start->setParentAnchor(line->point1);
arrow->end->setCoords(0, 0); //设置直线终点为坐标系中的点
arrow->setHead(QCPLineEnding::esSpikeArrow);//设置箭头类型(三角形、菱形、方形等)
arrow->setPen(linesPen);
arrow->setVisible(false);
QCPItemLine arrow2 = new QCPItemLine(this);
arrow2->start->setParentAnchor(line->point2);
arrow2->end->setCoords(0, 0); //设置直线终点为坐标系中的点
arrow2->setHead(QCPLineEnding::esSpikeArrow);//设置箭头类型(三角形、菱形、方形等)
arrow2->setPen(linesPen);
arrow2->setVisible(false);
/绘制游标文本
******/
textLabel = new QCPItemText(this);//在QCustomplot中新建文字框
textLabel->setPositionAlignment(Qt::AlignTop|Qt::AlignLeft);//文字布局:顶、左对齐
textLabel->position->setType(QCPItemPosition::ptAxisRectRatio);//位置类型(当前轴范围的比例为单位/实际坐标为单位)
textLabel->position->setCoords(0.1, 0.1); //把文字框放在X轴的中间,Y轴的最顶部
textLabel->setText(“Text Item Demo”);
textLabel->setColor(QColor(42,148,235));
textLabel->setFont(QFont(font().family(), 8)); //字体大小
textLabel->setPen(QPen(QColor(42,148,235))); //字体颜色
textLabel->setPadding(QMargins(2,2,2,2));//文字距离边框几个像素
textLabel->setVisible(false);
/**绘制数据吸附点/
tracer_h = new QCPItemTracer(this);
tracer_h->setGraph(this->graph(0));
tracer_h->setPen(QPen(Qt::green));
tracer_h->setStyle(QCPItemTracer::tsCircle);
connect(this, &QCustomPlot::mouseMove,this,[=](QMouseEvent *event)
{
double x = this->xAxis->pixelToCoord(event->pos().x());
double y = this->yAxis->pixelToCoord(event->pos().y());
pos= QPointF(x,y);
if(event->buttons() == Qt::LeftButton)
{
tracer_h->setGraphKey(x);
tracer_h->updatePosition();
line->point1->setCoords(x,this->yAxis->range().lower);
line->point2->setCoords(x,this->yAxis->range().upper);
line->setVisible(true);
arrow->start->setParentAnchor(line->point1);
arrow->end->setCoords(x,this->yAxis->range().upper);
arrow->setVisible(true);
arrow2->start->setParentAnchor(line->point2);
arrow2->end->setCoords(x,this->yAxis->range().lower);
arrow2->setVisible(true);
textLabel->position->setCoords(x/this->xAxis->range().upper,0.1);
textLabel->setVisible(true);
this->replot();
}

    });
}
if(dir == horiental)
{
    QCPItemStraightLine *line = new  QCPItemStraightLine (this);
    QPen linesPen(Qt::red, 1, Qt::DashLine);
    line->setPen(linesPen);
    line->setClipToAxisRect(true);
    line->point1->setCoords(0,0);
    line->point2->setCoords(0,0);
    line->setVisible(false);
    QCPItemLine *arrow = new QCPItemLine(this);
    arrow->start->setParentAnchor(line->point1);  //设置该直线的起点为文字框的下锚点
    arrow->end->setCoords(0, 0); //设置直线终点为坐标系中的点
    arrow->setHead(QCPLineEnding::esSpikeArrow);//设置箭头类型(三角形、菱形、方形等)
    arrow->setPen(linesPen);
    arrow->setVisible(false);
    QCPItemLine *arrow2 = new QCPItemLine(this);
    arrow2->start->setParentAnchor(line->point2);  //设置该直线的起点为文字框的下锚点
    arrow2->end->setCoords(0, 0); //设置直线终点为坐标系中的点
    arrow2->setHead(QCPLineEnding::esSpikeArrow);//设置箭头类型(三角形、菱形、方形等)
    arrow2->setPen(linesPen);
    arrow2->setVisible(false);
    textLabel2 = new QCPItemText(this);//在QCustomplot中新建文字框
    textLabel2->setPositionAlignment(Qt::AlignTop|Qt::AlignLeft);//文字布局:顶、左对齐
    textLabel2->position->setType(QCPItemPosition::ptAxisRectRatio);//位置类型(当前轴范围的比例为单位/实际坐标为单位)
    textLabel2->position->setCoords(0.1, 0.1); //把文字框放在X轴的中间,Y轴的最顶部
    textLabel2->setText("Text Item Demo");
    textLabel2->setFont(QFont(font().family(), 8)); //字体大小
    textLabel2->setPen(QPen(QColor(42,148,235))); //字体颜色
    textLabel2->setColor(QColor(42,148,235));
    textLabel2->setPadding(QMargins(2,2,2,2));//文字距离边框几个像素
    textLabel2->setVisible(false);
    QCPItemTracer *tracer_h = new QCPItemTracer(this);
    tracer_h->setGraph(this->graph(0));
    tracer_h->setPen(QPen(Qt::red));
    tracer_h->setStyle(QCPItemTracer::tsCircle);
    connect(this, &QCustomPlot::mouseMove,this,[=](QMouseEvent *event)
    {
        if(event->buttons() == Qt::RightButton)
        {
            double x = this->xAxis->pixelToCoord(event->pos().x());
            double y = this->yAxis->pixelToCoord(event->pos().y());
            pos2= QPointF(x,y);
            tracer_h->setGraphKey(x);
            tracer_h->updatePosition();
            line->point1->setCoords(this->xAxis->range().lower,y);
            line->point2->setCoords(this->xAxis->range().upper,y);
            line->setVisible(true);
            arrow->start->setParentAnchor(line->point1);  //设置该直线的起点为文字框的下锚点
            arrow->end->setCoords(this->xAxis->range().upper,y); //设置直线终点为坐标系中的点
            arrow->setVisible(true);
            arrow2->start->setParentAnchor(line->point2);  //设置该直线的起点为文字框的下锚点
            arrow2->end->setCoords(this->xAxis->range().lower,y); //设置直线终点为坐标系中的点
            arrow2->setVisible(true);
            textLabel2->position->setCoords(0.1,(1-y/this->yAxis->range().upper));
            textLabel2->setVisible(true);
            this->replot();
        }
    });
}

}
paintevent重载实现实时显示跟随的点
void SpectrumDisplay:: paintEvent(QPaintEvent *ev)
{
QCustomPlot::paintEvent(ev);
if(textLabel!=nullptr)
textLabel->setText(“MHZ:”+QString::number(pos.x())+";dB:"+QString::number(tracer_h->position->value()));
if(textLabel2!=nullptr)
textLabel2->setText(“MHZ:”+QString::number(pos2.x())+";dB:"+QString::number(tracer_h->position->value()));

//    QPainter pen(this);
//    pen.setRenderHint(QPainter::Antialiasing, true);
//    pen.setPen(Qt::yellow);
//    QRect a;
//    double w= this->width();
//    double h = this->height();
//    pen.drawRect(1,1,w/2,h/2);

}
/**

  • @brief SpectrumDisplay::TargetTraceRectangleTracking
  • @param data 绘制矩形追踪标识
  • @param sentce
    */
    void SpectrumDisplay:: TargetTraceRectangleTracking(QVector data,QVectorsentce)
    {
    for(int i =0;i<=sentce.count()-1;i++)
    {
    rect = new QCPItemRect(this);
    rect->topLeft->setCoords(data[sentce[i].x()].x(),data[sentce[i].y()].y()+30);
    rect->bottomRight->setCoords(data[sentce[i].y()].x(), data[sentce[i].y()].y()-20);
    rect->setPen(QPen(QColor(0,161,214)));
    }

}
void SpectrumDisplay:: drawDataEnvelop(QVector freq,QVectorappitude,QColor color)
{
appitlude_fre<<appitude;
static int z =0;
z++;
if(z>200)
{
appitlude_fre.clear();
z=0;
}
QVector maxapplitude(freq.length());
for(int i = 0;i<appitude.length();i++)
{
if(appitlude_fre.length()<2)
{
/增加一条曲线绘制包络**********/
}else
{
for(int j = 0;j<appitude.length();j++)
{
if(appitude[j]<appitlude_fre[0][j])
{
appitude[j] = appitlude_fre[0][j];

            }else {

            }
        }
        appitlude_fre.dequeue();
    }
}


this->addGraph();
this->graph(1);
this->graph(1)->setData(freq,appitude);
this->graph(1)->setPen(QPen( color));

}
void SpectrumDisplay:: mouseMoveEvent(QMouseEvent *event)
{
QCustomPlot::mouseMoveEvent(event);

}
绘制瀑布图
void wallplot:: SetWaterFallParm(double xmin,double xmax,double dBmin,double dBmax)
{

//this->setInteractions(QCP::iRangeDrag|QCP::iRangeZoom| QCP::iSelectPlottables);//可拖拽+可滚轮缩放



//每条曲线都会独占一个graph()


//通过传递的轴的QCustomPlot进行注册,简洁理解QCPColorMap的数据为(x轴,y轴;颜色,值value)
m_pColorMap = new QCPColorMap(this->xAxis,this->yAxis);
m_pColorMap->data()->setSize(1000,3000);//设置整个图(x,y)点数,X轴50个点,y轴4096个点
this->xAxis->setRange(xmin,xmax);//当前X轴显示范围
this->yAxis->setRange(-150,20);//当前y轴显示范围
m_pColorMap->data()->setRange(QCPRange(xmin,xmax),QCPRange(dBmin,dBmax));//setRange是设置X轴以及Y轴的范围
m_pColorMap->setGradient(QCPColorGradient::gpCold);//设置默认渐进色变化(可在QCPColorGradient中查看)
m_pColorMap->rescaleDataRange(true);

}
void wallplot:: setData(QVectordatax,QVectordatay)
{
static QList<QVector> data;
data<<datay;
if(data.length()>4)
{
data.removeLast();
}
this->addGraph();
this->graph(0)->setPen(QPen(Qt::gray));//曲线颜色

this->graph(0)->setBrush(QBrush(QColor(0,255,255,20)));//曲线与X轴包围区的颜色
//自动调整XY轴的范围,以便显示出graph(0)中所有的点
//给第一个graph设置rescaleAxes(),后续所有graph都设置rescaleAxes(true)即可实现显示所有曲线
this->graph(0)->rescaleAxes();
for(int x=0;x<datax.length();x++)
{
    for(int j=0;j<data.length();j++)
    {
        for(int y=0;y<data[j].length();y++)
        {
            m_pColorMap->data()->setCell(x,y*j+y,data[j][y]);
        }     
    }
}
this->replot();

}

在这里插入图片描述
这里就没加测试数据了,需要完整demo的可以私信我。

  • 10
    点赞
  • 35
    收藏
    觉得还不错? 一键收藏
  • 44
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 44
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值