该类可完成2D图形的绘制,下载地址为:
http://www.qcustomplot.com/index.php/download,推荐下载最上面一个的首个文件,里面有类,还有代码可以参考。
这个类我上传至
http://download.csdn.net/detail/zhulichen/9783916,大家可以下载下来使用
自己先新建一个工程,并将qcustomplot.h与qcustomplot.cpp拷贝到目录工程下,然后将该两个文件添加至工程中。同时需要在.pro文件 QT +=widgets 后面添加printsupport
在.ui界面中拖入一个widget区域,选中widget区域点击右键,选择“提升为”按钮,将提成的类名称。
现在运行下,将出现了简单的坐标系,该坐标系默认的坐标为(0,5)(0,5)
很多文章中显示的都是画线,我在这里写的程序是显示点,每次可以输入一个点,然后显示在坐标系。
在.h头文件中添加函数:
public
:
void updateWidgetShow(QCustomPlot *customPlot);
QPointF inputPoint(float x,float y);
在.cpp文件中添加
#include
"mainwindow.h"
#include "ui_mainwindow.h"
#include "qcustomplot.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
updateWidgetShow(ui->widget);
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::updateWidgetShow(QCustomPlot *customPlot)
{
customPlot->clearGraphs();
customPlot->setBackground(QPixmap("./background.jpg"));
//customPlot->axisRect()->setBackground(QPixmap("./background.jpg"));
customPlot->addGraph();
customPlot->graph()->setLineStyle(QCPGraph::lsNone);
customPlot->graph()->setScatterStyle(QCPScatterStyle(QPixmap("./sun.png")));
QPointF label=inputPoint(5.9,2.3);
QVector<double> xx(1),yy(1);
xx[0]=label.x();
yy[0]=label.y();
customPlot->graph()->addData(xx,yy);
customPlot->replot();
customPlot->xAxis->setRange(0,10);
customPlot->yAxis->setRange(0,10);
}
QPointF MainWindow::inputPoint(float x, float y)
{
QPointF point;
point.setX(x);
point.setY(y);
return point;
}
运行一下,结果如下图所示: