QT 曲线/示波器 customplot控件

1、使用

官方网站:http://www.qcustomplot.com/

把qcustomplot.cpp和qcustomplot.h拷贝到工程目录下,然后把这两个文件引入工程项目即可在项目中点右键添加现有文件,把两个文件加入工程。这时pro文件会添加上qcustomplot.cpp和qcustomplot.h,这时还需要添加的是

QT += widgets printsupport

在UI文件拖入一个Widget,命名为 Plot ,后续程序中操作的qcustomplot都是他。然后提升为QCustomPlot,.h文件同名自动变小写

2、绘图

    ui->Plot->addGraph();//添加曲线
    ui->Plot->graph(0)->setPen(QPen(Qt::blue));//设置曲线颜色
    qDebug()<<ui->Plot->graph(0)->name();//读取曲线名称   由于现在没设置,读取的名称为 "Graph 1"
    ui->Plot->graph(0)->setName(QString("New graph "));//设置曲线名称   现在再读取就是New graph 
    ui->Plot->graph(0)->setData(x, y0);//写入数据
	ui->Plot->graph(0)->addData(x,y0);//增添数据
	ui->Plot->graph(0)->data().data()->clear();//清空数据保留曲线
/*坐标轴*/
    ui->Plot->xAxis2->setVisible(true);//显示上方X轴
    ui->Plot->xAxis2->setTickLabels(true);//显示上方X轴 刻度
    ui->Plot->yAxis2->setVisible(true);//显示右侧Y轴
    ui->Plot->yAxis2->setTickLabels(false);//不显示右侧Y轴 刻度
    ui->Plot->xAxis->setVisible(true);//显示下方X轴
    ui->Plot->xAxis->setTickLabels(true);//显示下方X轴 刻度
    ui->Plot->yAxis->setVisible(true);//显示左侧Y轴
    ui->Plot->yAxis->setTickLabels(false);//不显示左侧Y轴 刻度
    ui->Plot->setInteractions(QCP::iRangeDrag | QCP::iRangeZoom | QCP::iSelectPlottables);//放大拖拽选中等
	ui->Plot->xAxis->setRange(0, 100);                         //当前X轴显示的范围
	ui->Plot->yAxis->setRange(0, 10);                          //当前Y轴显示的范围	
	ui->Plot->graph(0)->rescaleAxes();//坐标轴自适应	
ui->Plot->graph(1)->rescaleAxes(ture);//nlyEnlarge 默认false,表示范围可以缩小放大,如果为true表示只能放大,而不会缩小范围。
	//由于自适应坐标轴只会改变下和左轴,这样可以保证上下一致,左右一致
     connect(ui->Plot->xAxis, SIGNAL(rangeChanged(QCPRange)), ui->Plot->xAxis2, SLOT(setRange(QCPRange)));
     connect(ui->Plot->yAxis, SIGNAL(rangeChanged(QCPRange)), ui->Plot->yAxis2, SLOT(setRange(QCPRange)));
     QCPRange XAxis_Range=ui->Plot->xAxis->range();//获取调整前坐标轴数值
/*图例*/
	ui->Plot->legend->setVisible(true);//设置图例可见
// 设置图例行优先排列   默认图标会竖着排列,这样是横着
    ui->Plot->legend->setFillOrder(QCPLayoutGrid::foColumnsFirst);
// 设置六个图例自动换行
    ui->Plot->legend->setWrap(6);
// 设置图例位置,这里选择显示在QCPAxisRect下方,同理可设置显示在QCustomPlot中任意位置,还有比例
    ui->Plot->plotLayout()->addElement(1 , 0, ui->Plot->legend);
// 设置显示比例,图例所在框的大小
    ui->Plot->plotLayout()->setRowStretchFactor(1, 0.001);
// 设置边框隐藏,图例和曲线之间是有框的
    ui->Plot->legend->setBorderPen(Qt::NoPen);
//放大拖拽选中等枚举
enum Interaction { iRangeDrag         = 0x001 //左键点击可拖动
                   ,iRangeZoom        = 0x002 //范围可通过鼠标滚轮缩放
                   ,iMultiSelect      = 0x004 //可选中多条曲线
                   ,iSelectPlottables = 0x008 //线条可选中
                   ,iSelectAxes       = 0x010 //坐标轴可选
                   ,iSelectLegend     = 0x020 //图例是可选择的
                   ,iSelectItems      = 0x040 //可选择项(矩形、箭头、文本项等
                   ,iSelectOther      = 0x080 //所有其他对象都是可选的
                 };
    //设置画布背景色
        QLinearGradient plotGradient;
        plotGradient.setStart(0, 0);
        plotGradient.setFinalStop(0, 350);
        plotGradient.setColorAt(0, QColor(80, 80, 80));
        plotGradient.setColorAt(1, QColor(50, 50, 50));
    ui->Plot->setBackground(plotGradient);

3.拓展功能

3.1 设置图例与曲线同步选中

在maindows中,定义槽函数selectionChanged。

//H文件
private slots:
    void selectionChanged();
//cpp文件
void MainWindow::selectionChanged()
{
      // 同步选择图形与选择相应的图例项:
      for (int i=0; i<ui->Plot->graphCount(); ++i)
      {
        QCPGraph *graph = ui->Plot->graph(i);
        QCPPlottableLegendItem *item = ui->Plot->legend->itemWithPlottable(graph);
        if (item->selected() || graph->selected())
        {
          item->setSelected(true);        //完成图例选中
          graph->setSelection(QCPDataSelection(graph->data()->dataRange())); //完成曲线选中
        }
      }
}
//MainWindow函数中链接信号和槽函数
connect(ui->Plot, SIGNAL(selectionChangedByUser()), this, SLOT(selectionChanged()));

3.2 将原有的左键平移全键区域缩放改为左键平移,右键缩放

1、由于官方默认左键是平移曲线,我们先把平移功能改到右键(或者滚轮中键,根据你的喜好)上去,直接在这个函数里
void QCPAxisRect::mousePressEvent(QMouseEvent *event, const QVariant &details)改就行了,把Qt::LeftButton改成Qt::RightButton即可。

2、在QCustomPlot类中添加private变量:(可搜索 class QCP_LIB_DECL QCustomPlot)

private:
  QRubberBand *rb;
  QPoint startPos;
  bool cancelRb;

3、在QCustomPlot的3个鼠标事件函数添加代码

//0、在构造函数QCustomPlot::QCustomPlot(QWidget *parent)的初始化列表中添加:
  ,rb(new QRubberBand(QRubberBand::Rectangle, this))
  ,startPos(0, 0)
//1、左键按下时,记录坐标起点
    在QCustomPlot::mousePressEvent(QMouseEvent *event)中添加:
    if(event->buttons() & Qt::LeftButton)
    {
        startPos = event->pos();
        cancelRb = false;
        rb->resize(0, 0);
        rb->show();
    }
 
//2、左键按下并移动时,绘制矩形框
    在void QCustomPlot::mouseMoveEvent(QMouseEvent *event)中添加:
 
    if(event->buttons() & Qt::LeftButton)
    {
        QRect normalRect = QRect(startPos, event->pos()).normalized();//任意两点定义矩形
        rb->setGeometry(normalRect);
    }
//3、左键弹起时,记录终点坐标,并把曲线放大到【起点、终点】围成的矩形框中
    在void QCustomPlot::mouseReleaseEvent(QMouseEvent *event)中添加:
 
    if(event->button() == Qt::LeftButton)
    {
        rb->hide();
        if(!cancelRb)
        {
            QRect normalRect = QRect(startPos, event->pos()).normalized();
            rb->setGeometry(normalRect);
            this->xAxis->setRange(xAxis->pixelToCoord(normalRect.left()),
                                  xAxis->pixelToCoord(normalRect.right()));
 
            this->yAxis->setRange(yAxis->pixelToCoord(normalRect.bottom()),
                                  yAxis->pixelToCoord(normalRect.top()));
            this->replot();//立即刷新图像
        }
    }

3.3坐标轴示波器式移动

             /*设置X轴固定长度,示波器式推进*/
             if(ui->spinBox_X_Lenth->value()!=0)
             {
                 QCPRange XAxis_Range=ui->Plot->xAxis->range();//获取调整前坐标轴数值
                 if(XAxis_Range.upper-XAxis_Range.lower>ui->spinBox_X_Lenth->value())
                 {
                    XAxis_Range.lower=XAxis_Range.upper-ui->spinBox_X_Lenth->value();
                    ui->Plot->xAxis->setRange(XAxis_Range);
                 }
             }

3.4更改曲线风格

/*例程*/
connect(ui->spinBox_Line_Style_Point_Size,static_cast<void (QSpinBox::*)(const QString &)>(&QSpinBox::valueChanged),[=](){
        ui->Plot->graph(ui->comboBox_Line_Style_LineNum->currentIndex())->setScatterStyle(QCPScatterStyle(QCPScatterStyle::ScatterShape(ui->comboBox_Line_Style_Point->currentIndex()),ui->spinBox_Line_Style_Point_Size->value()));
        ui->Plot->replot();
    });
/*注*/
QCPScatterStyle::ScatterShape(ui->comboBox_Line_Style_Point->currentIndex()对应
==》QCPScatterStyle::ssDisc//这是取枚举的方式    QCPScatterStyle类ScatterShape
/*原*/ 
ui->Plot->graph(0)->setScatterStyle(QCPScatterStyle(QCPScatterStyle::ssDisc,5));
//QCPScatterStyle::ssDisc点样式
setScatterStyle(QCPScatterStyle(点样式,点大小));
  • 10
    点赞
  • 74
    收藏
    觉得还不错? 一键收藏
  • 5
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值