QCustomPlot的基本使用[通俗易懂]

QCustomPlot的基本使用[通俗易懂]QCustomPlot是QT下一个方便易用的绘图工具,只有两个文件qcustomplot.h和qcustomplot.cpp组成。源文件和使用文档可从官方网站下载。官方网站:http://www.qcustomplot.com/下面介绍下基本使用:1、将qcustomplot.cpp和qcustomplot.h拷贝到工程目录下,并在工程中添加文件。并在工程的pro文

大家好,又见面了,我是你们的朋友全栈君。如果您正在找激活码,请点击查看最新教程,关注关注公众号 “全栈程序员社区” 获取激活教程,可能之前旧版本教程已经失效.最新Idea2022.1教程亲测有效,一键激活。

Jetbrains全系列IDE稳定放心使用

QCustomPlot是QT下一个方便易用的绘图工具,只有两个文件qcustomplot.h和qcustomplot.cpp组成。源文件和使用文档可从官方网站下载。

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


下面介绍下基本使用:

1、将qcustomplot.cpp和qcustomplot.h拷贝到工程目录下,并在工程中添加文件。

QCustomPlot的基本使用[通俗易懂]QCustomPlot的基本使用[通俗易懂]

QCustomPlot的基本使用[通俗易懂]QCustomPlot的基本使用[通俗易懂]

并在工程的pro文件添加printsupport

greaterThan(QT_MAJOR_VERSION, 4): QT += widgets printsupport

QCustomPlot的基本使用[通俗易懂]

2、在QT里面添加一个Widget命名为qcustomplotWidget,对这个Widget右击,点击Promote to…

QCustomPlot的基本使用[通俗易懂]

QCustomPlot的基本使用[通俗易懂]

提升类名为QCustomPlot,并点击Add:

QCustomPlot的基本使用[通俗易懂]

QCustomPlot的基本使用[通俗易懂]

最后选中点击Promote:

QCustomPlot的基本使用[通俗易懂]

QCustomPlot的基本使用[通俗易懂]

然后就可以在工程中通过ui->qcustomplotWidget直接使用了。


3、通过本人项目来展示相关代码,实际参考了官网上下载的文档和例程

设置x,y轴:

   ui->qcustomplot->setInteractions(QCP::iRangeDrag | QCP::iRangeZoom | QCP::iSelectAxes |	//设置交互方式
                                      QCP::iSelectLegend | QCP::iSelectPlottables);
//    ui->qcustomplot->axisRect()->setupFullAxesBox();
    QBrush qBrush(QColor(255,255,255));//设置背景色
    ui->qcustomplot->setBackground(qBrush);

    ui->qcustomplot->legend->setVisible(true);
    ui->qcustomplot->xAxis->setLabel("Time Axis (t/s)");//设置x轴
    ui->qcustomplot->xAxis->setTicks(false);

    ui->qcustomplot->yAxis->setLabel("EEG Channel");//设置y轴
    ui->qcustomplot->yAxis->setAutoTicks(true);
    ui->qcustomplot->yAxis->setAutoTickStep(true);
    ui->qcustomplot->yAxis->setAutoSubTicks(true);
    ui->qcustomplot->yAxis->setRange(8000.0,10000.0);

x,y轴更多设置可参考QCPAxis Class。


添加图层:

<pre name="code" class="cpp">    graph1 = ui->qcustomplot->addGraph();//增加一条曲线图层
    graph2 = ui->qcustomplot->addGraph();//增加一条曲线图层


//QCPScatterStyle QCPcs1(QCPScatterStyle::ssSquare, QColor(255,0,0),QColor(255,0,0),3);//设置折线图的点的形状及颜色
    QPen qPen1(QColor(255,0,0));
//   graph_1->setScatterStyle(QCPcs1);
    graph1->setPen(qPen1);//设置画笔颜色
    graph1->setData(x,y);
    graph1->setName(QString("F3"));


//QCPScatterStyle QCPcs2(QCPScatterStyle::ssCircle, QColor(0,255,0),QColor(0,255,0),3);//设置折线图的点的形状及颜色
    QPen qPen2(QColor(0,255,0));
//   graph2->setScatterStyle(QCPcs2);
    graph2->setPen(qPen2);//设置画笔颜色
    graph2->setData(x,y);
    graph2->setName(QString("F4"));


图层更多使用情况可参考QCustomPlot Class。

添加数据:

可用graph1->addData()函数,具体函数参数为

void  addData (const QCPDataMap &dataMap) 
void  addData (const QCPData &data) 
void  addData (double key, double value) 
void  addData (const QVector< double > &keys, const QVector< double > &values) 


4、如果要实现对图的缩放移动,可以添加一下槽函数:

/**
 * @brief MainWindow::mousePress
 * 鼠标点击
 */
void MainWindow::mousePress()
{
  // if an axis is selected, only allow the direction of that axis to be dragged
  // if no axis is selected, both directions may be dragged
  if (ui->qcustomplotWidget->xAxis->selectedParts().testFlag(QCPAxis::spAxis))
  {
    ui->qcustomplotWidget->axisRect()->setRangeDrag(ui->qcustomplotWidget->xAxis->orientation());
  }
  else if (ui->qcustomplotWidget->yAxis->selectedParts().testFlag(QCPAxis::spAxis))
  {
    ui->qcustomplotWidget->axisRect()->setRangeDrag(ui->qcustomplotWidget->yAxis->orientation());
  }
  else
  {
    ui->qcustomplotWidget->axisRect()->setRangeDrag(Qt::Horizontal|Qt::Vertical);
  }
}

/**
 * @brief MainWindow::mouseWheel
 * 鼠标滚轮
 */
void MainWindow::mouseWheel()
{
  // if an axis is selected, only allow the direction of that axis to be zoomed
  // if no axis is selected, both directions may be zoomed
  if (ui->qcustomplotWidget->xAxis->selectedParts().testFlag(QCPAxis::spAxis))
  {
    ui->qcustomplotWidget->axisRect()->setRangeZoom(ui->qcustomplotWidget->xAxis->orientation());
  }
  else if (ui->qcustomplotWidget->yAxis->selectedParts().testFlag(QCPAxis::spAxis))
  {
    ui->qcustomplotWidget->axisRect()->setRangeZoom(ui->qcustomplotWidget->yAxis->orientation());
  }
  else
  {
    ui->qcustomplotWidget->axisRect()->setRangeZoom(Qt::Horizontal|Qt::Vertical);
  }
}

/**
 * @brief MainWindow::selectionChanged
 * 曲线选择
 */
void MainWindow::selectionChanged()
{
  /*
   normally, axis base line, axis tick labels and axis labels are selectable separately, but we want
   the user only to be able to select the axis as a whole, so we tie the selected states of the tick labels
   and the axis base line together. However, the axis label shall be selectable individually.

   The selection state of the left and right axes shall be synchronized as well as the state of the
   bottom and top axes.

   Further, we want to synchronize the selection of the graphs with the selection state of the respective
   legend item belonging to that graph. So the user can select a graph by either clicking on the graph itself
   or on its legend item.
  */

  // make top and bottom axes be selected synchronously, and handle axis and tick labels as one selectable object:
  if (ui->qcustomplotWidget->xAxis->selectedParts().testFlag(QCPAxis::spAxis) || ui->qcustomplotWidget->xAxis->selectedParts().testFlag(QCPAxis::spTickLabels) ||
      ui->qcustomplotWidget->xAxis2->selectedParts().testFlag(QCPAxis::spAxis) || ui->qcustomplotWidget->xAxis2->selectedParts().testFlag(QCPAxis::spTickLabels))
  {
    ui->qcustomplotWidget->xAxis2->setSelectedParts(QCPAxis::spAxis|QCPAxis::spTickLabels);
    ui->qcustomplotWidget->xAxis->setSelectedParts(QCPAxis::spAxis|QCPAxis::spTickLabels);
  }
  // make left and right axes be selected synchronously, and handle axis and tick labels as one selectable object:
  if (ui->qcustomplotWidget->yAxis->selectedParts().testFlag(QCPAxis::spAxis) || ui->qcustomplotWidget->yAxis->selectedParts().testFlag(QCPAxis::spTickLabels) ||
      ui->qcustomplotWidget->yAxis2->selectedParts().testFlag(QCPAxis::spAxis) || ui->qcustomplotWidget->yAxis2->selectedParts().testFlag(QCPAxis::spTickLabels))
  {
    ui->qcustomplotWidget->yAxis2->setSelectedParts(QCPAxis::spAxis|QCPAxis::spTickLabels);
    ui->qcustomplotWidget->yAxis->setSelectedParts(QCPAxis::spAxis|QCPAxis::spTickLabels);
  }

  // synchronize selection of graphs with selection of corresponding legend items:
  for (int i=0; i<ui->qcustomplotWidget->graphCount(); ++i)
  {
    QCPGraph *graph = ui->qcustomplotWidget->graph(i);
    QCPPlottableLegendItem *item = ui->qcustomplotWidget->legend->itemWithPlottable(graph);
    if (item->selected() || graph->selected())
    {
      item->setSelected(true);
      graph->setSelected(true);
    }
  }
}


并连接信号:

    connect(ui->qcustomplotWidget, SIGNAL(mousePress(QMouseEvent*)), this, SLOT(mousePress()));//连接鼠标点击信号和槽
    connect(ui->qcustomplotWidget, SIGNAL(mouseWheel(QWheelEvent*)), this, SLOT(mouseWheel()));//连接鼠标滚轮信号和槽
    connect(ui->qcustomplotWidget, SIGNAL(selectionChangedByUser()), this, SLOT(selectionChanged()));//连接曲线选择信号和槽

最后效果图:

QCustomPlot的基本使用[通俗易懂]

QCustomPlot的基本使用[通俗易懂]



版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/189490.html原文链接:https://javaforall.net

(0)
上一篇 2026年4月13日 下午3:25
下一篇 2026年4月13日 下午3:31


相关推荐

  • 5.16 综合案例2.0-久坐提醒系统(2.2版本接口有更新)

    5.16 综合案例2.0-久坐提醒系统(2.2版本接口有更新)综合案例2.0-久坐提醒系统简介准备硬件连接图代码流程功能实现1、物联网平台开发2、设备端开发3、调试调试结果4、钉钉消息提醒4.1添加钉钉机器人4.2、IoTStudio设置简介长期久坐会损害身体健康,本案例就是为了提醒人们不要坐太久而设计的一个提醒系统。当你长时间在工位上坐着,他会通过顶顶提醒你,让你每隔一段时间活动一下筋骨。久坐提醒设备是通过人体红外检测周围区域是否有人移动,当累计检测时长超过设定值,将会在钉钉群发来提醒,每次回到座位会重新开始计时。并且提醒时间可以自行调节,默认30分钟。准备

    2022年10月1日
    7
  • 文件上传-文件名长度绕过白名单限制[通俗易懂]

    文件上传-文件名长度绕过白名单限制[通俗易懂]  在实际***中,当我们发现有文件上传的地方时,我们会尽可能地尝试所有的办法进行webshell的上传,只要能上传webshell,就说明本次***至少成功了一半,后续就看获得的webshell的权限情况进行下一步的操作。对于文件上传漏洞的防护来说,主要分为以下两类:白名单限制和黑名单限制,对于黑名单的限制,我们只需要寻找一些较为偏僻的可执行后缀、大小写混写以及相关操作系统的特性(如windo…

    2022年10月19日
    4
  • python爬虫—–request模块学习及案例

    python爬虫—–request模块学习及案例

    2021年4月15日
    168
  • 关于bootstrap模版Bootstrapper的问题「建议收藏」

    关于bootstrap模版Bootstrapper的问题「建议收藏」模版来源http://www.gbin1.com/tools/websitetemplate/20130111-free-template-for-bootstrap/我将模版源码未更改的情况下直接上传到服务器,结果页面显示有的时候有问题 问题图片如下 正常的内容应该是这样的网站地址 :http://3.freepander.duap

    2022年7月20日
    21
  • 0-1背包问题 回溯法

    0-1背包问题 回溯法0 1 背包问题回溯法

    2026年3月26日
    2
  • 欢欣雀跃还是欢呼雀跃_极空间 docker

    欢欣雀跃还是欢呼雀跃_极空间 dockerAnaconda清华镜像已经恢复使用!目录1.Anaconda清华源终于活了!2.配置命令:2.1添加清华anaconda镜像:2.1Conda附加库:conda-forgemsys2biocondamenpopytorch1.Anaconda清华源终于活了!2.配置命令:2.1添加清华anaconda镜像:condaconfig–addchannelsh…

    2025年7月22日
    4

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号