admin管理员组

文章数量:1590153

Interaction实例中主要简单的使用了跟用户交互有关的一些plottables的信号和常用函数的调用。
主要用到的部件有QCPPlotTitle,QCPAxis,QCPLegend,QCPPlottableLegendItem,QCPGraph。
主要用到的函数
selectedParts()选中的部分
testFlag()测试选中的是否是某一部分
setSelectedParts()设置选中的部分
setRangeDrag()设置可以进行范围拖动的方向
setRangeZoom()设置可以缩放的方向
selectedGraphs()选中的graphs

clearGraphs()清除所有的graphs

//mainwindow.cpp文件简单解析

#include "mainwindow.h"
#include "ui_mainwindow.h"

//用户交互实例
MainWindow::MainWindow(QWidget *parent) :
  QMainWindow(parent),
  ui(new Ui::MainWindow)
{
  srand(QDateTime::currentDateTime().toTime_t());//设置随机种子
  ui->setupUi(this);
  
  //设置用户可以对customPlot进行那些操作
  ui->customPlot->setInteractions(QCP::iRangeDrag | QCP::iRangeZoom | QCP::iSelectAxes |
                                  QCP::iSelectLegend | QCP::iSelectPlottables);
  //设置x,y轴的坐标范围
  ui->customPlot->xAxis->setRange(-8, 8);
  ui->customPlot->yAxis->setRange(-5, 5);
  //设置四个坐标轴都可见
  ui->customPlot->axisRect()->setupFullAxesBox();
  //在customPlot中插入一行并且添加一个标题元素
  ui->customPlot->plotLayout()->insertRow(0);
  ui->customPlot->plotLayout()->addElement(0, 0, new QCPPlotTitle(ui->customPlot, "Interaction Example"));
  //设置x,y轴的标签并且设置图例可见
  ui->customPlot->xAxis->setLabel("x Axis");
  ui->customPlot->yAxis->setLabel("y Axis");
  ui->customPlot->legend->setVisible(true);
  //对图例进行一些字体大小,以及图例框中只有Item能选中的设置
  QFont legendFont = font();
  legendFont.setPointSize(10);
  ui->customPlot->legend->setFont(legendFont);
  ui->customPlot->legend->setSelectedFont(legendFont);
  ui->customPlot->legend->setSelectableParts(QCPLegend::spItems); // legend box shall not be selectable, only legend items
  
  //添加四个随机的图形
  addRandomGraph();
  addRandomGraph();
  addRandomGraph();
  addRandomGraph();
  //绑定一些信号跟槽
  // connect slot that ties some axis selections together (especially opposite axes):
  connect(ui->customPlot, SIGNAL(selectionChangedByUser()), this, SLOT(selectionChanged()));
  // connect slots that takes care that when an axis is selected, only that direction can be dragged and zoomed:
  connect(ui->customPlot, SIGNAL(mousePress(QMouseEvent*)), this, SLOT(mousePress()));
  connect(ui->customPlot, SIGNAL(mouseWheel(QWheelEvent*)), this, SLOT(mouseWheel()));
  
  // make bottom and left axes transfer their ranges to top and right axes:
  connect(ui->customPlot->xAxis, SIGNAL(rangeChanged(QCPRange)), ui->customPlot->xAxis2, SLOT(setRange(QCPRange)));
  connect(ui->customPlot->yAxis, SIGNAL(rangeChanged(QCPRange)), ui->customPlot->

本文标签: 简单QCustomPlotInteraction