我只需要通过qt工具打印2d图。我不能使用qCustomPlot或qwt代替。我在qt里面看到了QChart库,我使用5.12 qt版本。
作为参考,我使用了动态显示图,它起作用了。但是我必须手动添加点,并且当我这样做时,qchart不会重新绘制。有参考文献https://doc.qt.io/archives/qt-5.10/qtcharts-dynamicspline-example.html。我还在堆栈溢出中找到了解决此问题的方法,但对我而言不起作用。我在qchart中添加了点之后做了update(),并且qchartview的update()也不能正常工作。但是,如果我在qchart的解释器中添加点,它将起作用。我还在QSplineSeries中做了addPoint(double,double),这是为了不重新绘制。
有效(在构造函数中)
{
addSeries(m_series);
m_series->append(m_x, m_y);
m_series->append(m_x + 0.1, m_y + 0.1);
m_series->append(m_x + 0.4, m_y + 0.4);
m_series->append(m_x + 0.6, m_y + 0.1);
}
不起作用(在同一类的另一个方法中)
void Chart::addPoint(double _arg, double _func)
{
dataFunct.append(QPointF(_arg,_func));
m_series->clear();
removeSeries(m_series);
//QSplineSeries* spLine = dynamic_cast<QSplineSeries*>(tmpSeria);
*m_series<<dataFunct;
addSeries(m_series);
}
我想在新的绘图点插入之后,在状态期间显示qchart。点是在另一个函数中计算的。
这是我图表的来源。
#ifndef CHAT_H
#define CHAT_H
#include <QtCharts/QChart>
#include <QtCore/QTimer>
QT_CHARTS_BEGIN_NAMESPACE
class QSplineSeries;
class QValueAxis;
QT_CHARTS_END_NAMESPACE
QT_CHARTS_USE_NAMESPACE
class Chart: public QChart
{
Q_OBJECT
public:
Chart(QGraphicsItem *parent = 0, Qt::WindowFlags wFlags = 0);
virtual ~Chart();
void setPoint(double _arg, double _func);
signals:
void addPointSignal(double,double);
public slots:
void addPoint(double _arg, double _func);
private:
QSplineSeries *m_series;
QStringList m_titles;
QValueAxis *m_axis;
qreal m_step;
qreal m_x;
qreal m_y;
QList<QPointF> dataFunct;
};
#endif // CHAT_H
源
#include "chart.h"
#include <QtCharts/QAbstractAxis>
#include <QtCharts/QSplineSeries>
#include <QtCharts/QValueAxis>
#include <QtCore/QRandomGenerator>
#include <QtCore/QDebug>
Chart::Chart(QGraphicsItem *parent, Qt::WindowFlags wFlags):
QChart(QChart::ChartTypeCartesian, parent, wFlags),
m_series(0),
m_axis(new QValueAxis),
m_step(0),
m_x(0.0),
m_y(0.0)
{
m_series = new QSplineSeries(this);
QPen red(Qt::red);
red.setWidth(3);
m_series->setPen(red);
addSeries(m_series);
//m_series->append(m_x, m_y);
//m_series->append(m_x + 0.1, m_y + 0.1);
//m_series->append(m_x + 0.4, m_y + 0.4);
//m_series->append(m_x + 0.6, m_y + 0.1);
createDefaultAxes();
setAxisX(m_axis, m_series);
m_axis->setTickCount(5);
axisX()->setRange(-1, 3);
axisY()->setRange(-2, 2);
}
Chart::~Chart()
{
}
void Chart::setPoint(double _arg, double _func)
{
}
void Chart::addPoint(double _arg, double _func)
{
dataFunct.append(QPointF(_arg,_func));
//removeAllSeries();
m_series->clear();
removeSeries(m_series);
//QSplineSeries* spLine = dynamic_cast<QSplineSeries*>(tmpSeria);
*m_series<<dataFunct;
addSeries(m_series);
}