QCustomPlot的基本使用

本文介绍QCustomPlot的基本使用方法,包括组件的引入、配置及如何添加数据。此外,还展示了如何设置交互功能,如缩放和平移等。

QCustomPlot的基本使用

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

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

 

下面介绍下基本使用:

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

 

并在工程的pro文件添加printsupport

 

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

 

 

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

 

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

 

最后选中点击Promote:

 

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

 

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

设置x,y轴:

 


 
  1. ui->qcustomplot->setInteractions(QCP::iRangeDrag | QCP::iRangeZoom | QCP::iSelectAxes | //设置交互方式

  2. QCP::iSelectLegend | QCP::iSelectPlottables);

  3. // ui->qcustomplot->axisRect()->setupFullAxesBox();

  4. QBrush qBrush(QColor(255,255,255));//设置背景色

  5. ui->qcustomplot->setBackground(qBrush);

  6.  
  7. ui->qcustomplot->legend->setVisible(true);

  8. ui->qcustomplot->xAxis->setLabel("Time Axis (t/s)");//设置x轴

  9. ui->qcustomplot->xAxis->setTicks(false);

  10.  
  11. ui->qcustomplot->yAxis->setLabel("EEG Channel");//设置y轴

  12. ui->qcustomplot->yAxis->setAutoTicks(true);

  13. ui->qcustomplot->yAxis->setAutoTickStep(true);

  14. ui->qcustomplot->yAxis->setAutoSubTicks(true);

  15. ui->qcustomplot->yAxis->setRange(8000.0,10000.0);

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

 

 

添加图层:

 


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

  2.     graph2 = ui->qcustomplot->addGraph();//增加一条曲线图层

  3.  
  4.  
  5. //QCPScatterStyle QCPcs1(QCPScatterStyle::ssSquare, QColor(255,0,0),QColor(255,0,0),3);//设置折线图的点的形状及颜色

  6.     QPen qPen1(QColor(255,0,0));

  7. //   graph_1->setScatterStyle(QCPcs1);

  8.     graph1->setPen(qPen1);//设置画笔颜色

  9.     graph1->setData(x,y);

  10.     graph1->setName(QString("F3"));

  11.  
  12.  
  13. //QCPScatterStyle QCPcs2(QCPScatterStyle::ssCircle, QColor(0,255,0),QColor(0,255,0),3);//设置折线图的点的形状及颜色

  14.     QPen qPen2(QColor(0,255,0));

  15. //   graph2->setScatterStyle(QCPcs2);

  16.     graph2->setPen(qPen2);//设置画笔颜色

  17.     graph2->setData(x,y);

  18.     graph2->setName(QString("F4"));

 


 

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

 

 

添加数据:

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

 


 
  1. void addData (const QCPDataMap &dataMap)

  2. void addData (const QCPData &data)

  3. void addData (double key, double value)

  4. void addData (const QVector< double > &keys, const QVector< double > &values)

  5.  


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

 

 


 
  1. /**

  2. * @brief MainWindow::mousePress

  3.  * 鼠标点击

  4. */

  5. void MainWindow::mousePress()

  6. {

  7. // if an axis is selected, only allow the direction of that axis to be dragged

  8. // if no axis is selected, both directions may be dragged

  9. if (ui->qcustomplotWidget->xAxis->selectedParts().testFlag(QCPAxis::spAxis))

  10. {

  11. ui->qcustomplotWidget->axisRect()->setRangeDrag(ui->qcustomplotWidget->xAxis->orientation());

  12. }

  13. else if (ui->qcustomplotWidget->yAxis->selectedParts().testFlag(QCPAxis::spAxis))

  14. {

  15. ui->qcustomplotWidget->axisRect()->setRangeDrag(ui->qcustomplotWidget->yAxis->orientation());

  16. }

  17. else

  18. {

  19. ui->qcustomplotWidget->axisRect()->setRangeDrag(Qt::Horizontal|Qt::Vertical);

  20. }

  21. }

  22.  
  23. /**

  24. * @brief MainWindow::mouseWheel

  25.  * 鼠标滚轮

  26. */

  27. void MainWindow::mouseWheel()

  28. {

  29. // if an axis is selected, only allow the direction of that axis to be zoomed

  30. // if no axis is selected, both directions may be zoomed

  31. if (ui->qcustomplotWidget->xAxis->selectedParts().testFlag(QCPAxis::spAxis))

  32. {

  33. ui->qcustomplotWidget->axisRect()->setRangeZoom(ui->qcustomplotWidget->xAxis->orientation());

  34. }

  35. else if (ui->qcustomplotWidget->yAxis->selectedParts().testFlag(QCPAxis::spAxis))

  36. {

  37. ui->qcustomplotWidget->axisRect()->setRangeZoom(ui->qcustomplotWidget->yAxis->orientation());

  38. }

  39. else

  40. {

  41. ui->qcustomplotWidget->axisRect()->setRangeZoom(Qt::Horizontal|Qt::Vertical);

  42. }

  43. }

  44.  
  45. /**

  46. * @brief MainWindow::selectionChanged

  47. * 曲线选择

  48. */

  49. void MainWindow::selectionChanged()

  50. {

  51. /*

  52. normally, axis base line, axis tick labels and axis labels are selectable separately, but we want

  53. the user only to be able to select the axis as a whole, so we tie the selected states of the tick labels

  54. and the axis base line together. However, the axis label shall be selectable individually.

  55.  
  56. The selection state of the left and right axes shall be synchronized as well as the state of the

  57. bottom and top axes.

  58.  
  59. Further, we want to synchronize the selection of the graphs with the selection state of the respective

  60. legend item belonging to that graph. So the user can select a graph by either clicking on the graph itself

  61. or on its legend item.

  62. */

  63.  
  64. // make top and bottom axes be selected synchronously, and handle axis and tick labels as one selectable object:

  65. if (ui->qcustomplotWidget->xAxis->selectedParts().testFlag(QCPAxis::spAxis) || ui->qcustomplotWidget->xAxis->selectedParts().testFlag(QCPAxis::spTickLabels) ||

  66. ui->qcustomplotWidget->xAxis2->selectedParts().testFlag(QCPAxis::spAxis) || ui->qcustomplotWidget->xAxis2->selectedParts().testFlag(QCPAxis::spTickLabels))

  67. {

  68. ui->qcustomplotWidget->xAxis2->setSelectedParts(QCPAxis::spAxis|QCPAxis::spTickLabels);

  69. ui->qcustomplotWidget->xAxis->setSelectedParts(QCPAxis::spAxis|QCPAxis::spTickLabels);

  70. }

  71. // make left and right axes be selected synchronously, and handle axis and tick labels as one selectable object:

  72. if (ui->qcustomplotWidget->yAxis->selectedParts().testFlag(QCPAxis::spAxis) || ui->qcustomplotWidget->yAxis->selectedParts().testFlag(QCPAxis::spTickLabels) ||

  73. ui->qcustomplotWidget->yAxis2->selectedParts().testFlag(QCPAxis::spAxis) || ui->qcustomplotWidget->yAxis2->selectedParts().testFlag(QCPAxis::spTickLabels))

  74. {

  75. ui->qcustomplotWidget->yAxis2->setSelectedParts(QCPAxis::spAxis|QCPAxis::spTickLabels);

  76. ui->qcustomplotWidget->yAxis->setSelectedParts(QCPAxis::spAxis|QCPAxis::spTickLabels);

  77. }

  78.  
  79. // synchronize selection of graphs with selection of corresponding legend items:

  80. for (int i=0; i<ui->qcustomplotWidget->graphCount(); ++i)

  81. {

  82. QCPGraph *graph = ui->qcustomplotWidget->graph(i);

  83. QCPPlottableLegendItem *item = ui->qcustomplotWidget->legend->itemWithPlottable(graph);

  84. if (item->selected() || graph->selected())

  85. {

  86. item->setSelected(true);

  87. graph->setSelected(true);

  88. }

  89. }

  90. }


并连接信号:

 

 


 
  1. connect(ui->qcustomplotWidget, SIGNAL(mousePress(QMouseEvent*)), this, SLOT(mousePress()));//连接鼠标点击信号和槽

  2. connect(ui->qcustomplotWidget, SIGNAL(mouseWheel(QWheelEvent*)), this, SLOT(mouseWheel()));//连接鼠标滚轮信号和槽

  3. connect(ui->qcustomplotWidget, SIGNAL(selectionChangedByUser()), this, SLOT(selectionChanged()));//连接曲线选择信号和槽

 

 

 

最后效果图:

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值