1.在官网下载customPlot所需的源码以及例子
https://www.qcustomplot.com/index.php/download
2.在原有的项目中添加customplot所需的源码:将.cpp和.h文件拷贝在工程目录下,在工程中添加文件
3.qcustomplot中包含打印的功能,在pro工程文件中添加printsupport。确认qcustomplot已添加进工程(旧版本可能需要手动添加)
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets printsupport
4.在ui界面中使用widget容器,在此容器中绘制曲线图。为方便将该对应(提升)到customPlot类。
在容器上电机鼠标右键,选择提升。再重命名控件为customPlot,就可以直接使用ui->customPlot进行操作
5.一个简单的图形
void MainWindow::PaintChart()
{
QVector<double> temp(10);
QVector<double> temp1(10);
for(int i=0;i<10;i++)
{
temp[i] = i;
if ((i + 1) % 2 == 0)
{
temp1[i] = i*2;
}else{
temp1[i] = i/2;
}
}
//添加一条曲线
ui->customPlot->addGraph();
ui->customPlot->graph(0)->setData(temp, temp1);
//设置x轴,y轴
ui->customPlot->xAxis->setLabel("x");
ui->customPlot->yAxis->setLabel("y");
//设置x,y轴的范围
ui->customPlot->xAxis->setRange(0, 10);
ui->customPlot->yAxis->setRange(0, 20);
ui->customPlot->replot();
}