需要插入的头文件
这些头文件都通过编译qwtplot6.1.3产生,编译见VS2015配置64位qwt6.1.3
#include <qwt_plot.h>
#include <qwt_plot_curve.h>
#include <qwt_plot_canvas.h>
#include <qdatetime.h>
#include <qwt_abstract_scale_draw.h>
#include <qwt_plot_grid.h>
#include <qwt_scale_widget.h>
#include <qwt_plot_layout.h>
#include <qwt_symbol.h>
初始变量定义
QVector<double> m_DataVectorX;
QVector<double> m_DataVectorY;
QwtPlot *m_ProfileQwtPlot;
QwtPlotCurve *m_ProfileCurve;
void ShowCurve();
函数实现
void Resonator::ShowCurve()
{
m_ProfileQwtPlot = new QwtPlot(this);
m_ProfileQwtPlot->move(20, 50);
m_ProfileQwtPlot->setFixedWidth(300); // 设置坐标轴的大小
m_ProfileQwtPlot->setFixedHeight(300);
m_ProfileQwtPlot->setTitle("Curve"); // 设置坐标轴的标题
m_ProfileQwtPlot->setAxisScale(QwtPlot::yLeft, -4, 4, 1); // 坐标轴的范围和间隔
m_ProfileQwtPlot->setAxisScale(QwtPlot::xBottom, -4, 4, 1);
m_ProfileQwtPlot->enableAxis(QwtPlot::xBottom, true);
m_ProfileQwtPlot->setStyleSheet("background:transparent;");//删除坐标轴背景
m_ProfileQwtPlot->setAxisMaxMinor(QwtPlot::xBottom, 5);
m_ProfileQwtPlot->setAxisMaxMinor(QwtPlot::yLeft, 5);//设置每一个大格有都少个小格
m_ProfileQwtPlot->setCanvasBackground(Qt::white); // 设置背景白色
for (int n = 0; n < m_ProfileQwtPlot->axisCnt; n++)
{
QwtScaleWidget *poScaleWidget = m_ProfileQwtPlot->axisWidget(n);
if (poScaleWidget)
{
poScaleWidget->setMargin(0);
}
QwtScaleDraw *poScaleDraw = m_ProfileQwtPlot->axisScaleDraw(n);
if (poScaleDraw)
{
poScaleDraw->enableComponent(QwtAbstractScaleDraw::Backbone, false);
}
}
m_ProfileQwtPlot->plotLayout()->setAlignCanvasToScales(true);
QwtPlotGrid *grid = new QwtPlotGrid();
grid->enableXMin(true);
grid->enableYMin(true);
grid->enableX(true);
grid->enableY(true);
grid->setMajorPen(QPen(Qt::gray, 0, Qt::SolidLine)); // 大网格
grid->setMinorPen(QPen(QColor(186, 186, 186), 0, Qt::DotLine));// 小网格
grid->attach(m_ProfileQwtPlot);
m_ProfileCurve = new QwtPlotCurve;
m_ProfileCurve->setPen(QColor(255, 85, 255), 2, Qt::SolidLine);
m_ProfileCurve->setCurveAttribute(QwtPlotCurve::Fitted, true);
m_ProfileCurve->setRenderHint(QwtPlotItem::RenderAntialiased, true);
QwtSymbol *symbol = new QwtSymbol(QwtSymbol::NoSymbol,
QBrush(Qt::white), QPen(Qt::red, 2), QSize(3, 3));//设置样本点的颜色、大小
m_ProfileCurve->setSymbol(symbol);
m_ProfileCurve->attach(m_ProfileQwtPlot);
//读数据画图
QFile fr("data/circle_noise.txt");
if (!fr.open(QIODevice::ReadOnly | QIODevice::Text))
{
qDebug() << "LLLLLLL" << endl;
return;
}
QTextStream in(&fr);
QStringList numList;
while (!in.atEnd())
{
numList = in.readLine().split(" ");
m_DataVectorX.push_back(numList[0].toDouble());
m_DataVectorY.push_back(numList[1].toDouble());
}
m_ProfileCurve->setSamples(m_DataVectorX, m_DataVectorY);
m_ProfileCurve->setPen(QPen(Qt::blue));
m_ProfileQwtPlot->replot();
}