首页 > 技术知识 > 正文

下载链接:http://www.qcustomplot.com/index.php/download

博主使用的是1.3.2的版本,最新的2.0的版本中有很多的东西已经更新,官方的使用说明存在出入,所以此次使用的是1.3.2的版本。

将下载好的文件解压,qcustomplot.cpp 和qcustomplot.h这两个文件,放入我们创建的工程里面。

下载的解压文件中有相应的example。可以先查看相应的PLOTS的工程文件。在QT中打开项目工程plot-examples.pro,进行查看可编译。

在自己创建的工程中:

customPlot = new QCustomPlot(); QVBoxLayout *vbox = new QVBoxLayout(); vbox->addWidget(customPlot); vbox->setSpacing(0); vbox->setContentsMargins(0,0,0,0);

先设置一下控件,以及设置布局方式。

customPlot->setInteractions(QCP::iRangeDrag | QCP::iRangeZoom | QCP::iSelectAxes | QCP::iSelectLegend | QCP::iSelectPlottables);

这部分是设置鼠标点击,鼠标滑轮,区域设置,标题设置等,可选的设置。 customPlot->setInteractions(QCP::iSelectLegend); //设置折线等的设置 customPlot->axisRect()->setupFullAxesBox(); //区域填充满坐标轴 customPlot->legend->setVisible(true); //折现描述的区域模块显示 QFont legendFont = font(); legendFont.setPointSize(20); //设置字体大小 customPlot->legend->setFont(legendFont); customPlot->xAxis->setTickLabelFont(legendFont); customPlot->yAxis->setTickLabelFont(legendFont); //设置XY轴字体大小

// 设置xy轴内的模式:

customPlot->addGraph(); customPlot->graph()->setLineStyle((QCPGraph::LineStyle::lsLine));

// 设置线的模式

customPlot->graph()->setScatterStyle(QCPScatterStyle::ScatterShape::ssDisc);

//设置线的节点的

QPen graphPen; graphPen.setColor(QColor(Qt::red)); graphPen.setWidthF(3); customPlot->graph()->setPen(graphPen); //设置线的颜色,宽度,以及画笔 customPlot->xAxis->setRange(0, 10); customPlot->xAxis->setAutoTickStep(false); customPlot->xAxis->setTickStep(3); customPlot->xAxis->setAutoSubTicks(false); customPlot->xAxis->setSubTickCount(3-1); customPlot->yAxis->setRange(0, 5); customPlot->yAxis->setAutoTickStep(false); customPlot->yAxis->setTickStep(1); // customPlot->yAxis->setSubTickCount(1); customPlot->xAxis->setLabel(“x_str”); customPlot->yAxis->setLabel(“y_str”);

//设置X轴和Y轴的范围

//设置XY轴的间隔的时候,需要先把自动设置间隔的关闭,然后手动设置。

//XY轴的标签也需要设置名称。 QVector<double> x, y; y << 0<< 2 << 3 << 5 << 7 << 3 << 8 << 2<<5; x <<0<< 3<< 6 << 9 << 12 << 15 << 18 << 21 <<24; customPlot->graph()->setData(x, y); customPlot->replot();

设置数据的时候,需要用到QVector,然后将数据存入,一个一个的显示。

在调用完之后需要使用customPlot->replot();将数据更新到坐标轴上去。

猜你喜欢