QtCharts setRange 问题
Posted
技术标签:
【中文标题】QtCharts setRange 问题【英文标题】:QtCharts setRange issue 【发布时间】:2020-06-27 08:28:14 【问题描述】:我想使用QtCharts
显示一些数据。当我设置我的数据时,什么都不会显示。我认为问题出在setRange
方法上。
代码:
chart.h:
#ifndef CHART_H
#define CHART_H
#include <QChart>
#include <QTimer>
#include <QAbstractAxis>
#include <QSplineSeries>
#include <QValueAxis>
#include <QDebug>
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();
public slots:
void handleTimeout();
private:
QTimer m_timer;
QSplineSeries *m_series;
QStringList m_titles;
QValueAxis *m_axisX;
QValueAxis *m_axisY;
qreal m_step;
qreal m_x;
qreal m_y;
QList<double> testList;
;
#endif /* CHART_H */
chart.cpp:
#include "chart.h"
Chart::Chart(QGraphicsItem *parent, Qt::WindowFlags wFlags):
QChart(QChart::ChartTypeCartesian, parent, wFlags),
m_series(0),
m_step(0),
m_x(1),
m_y(1)
m_axisX = new QValueAxis(this);
m_axisX->setLabelsVisible(false);
m_axisY = new QValueAxis(this);
m_axisY->setLabelsVisible(false);
connect(&m_timer, &QTimer::timeout, this, &Chart::handleTimeout);
m_timer.setInterval(1000);
m_series = new QSplineSeries(this);
QPen green(Qt::green);
green.setWidth(3);
m_series->setPen(green);
m_series->append(m_x, m_y);
legend()->hide();
addSeries(m_series);
setAxisX(m_axisX, m_series);
setAxisY(m_axisY, m_series);
m_axisX->setTickCount(5);
testList << 93.8436 << 777.797 << 2507.78 << 5999.44 << 6806.54 << 7481.16
<< 8008.5 << 8093.8 << 8161.83 << 8216.99
<< 8280.46 << 8328.4 << 8394.55 << 8469.84
<< 8500.65 << 8558.16 << 8660.9 << 8638.87
<< 8726.47 << 8715.25 << 8804.48 << 8793.86
<< 8839.42 << 8875.75 << 8938.24 << 8977.09
<< 9020.27 << 9046.7 << 9092.04 << 9121.58
<< 9155.36 << 9199.46;
axisX()->setRange(0, 100);
axisY()->setRange(-1, 100);
m_timer.start();
void Chart::handleTimeout()
qreal x = plotArea().width() / m_axisX->tickCount();
qreal y = (m_axisX->max() - m_axisX->min()) / m_axisX->tickCount();
m_x += y;
if (!testList.isEmpty())
m_y = testList.first();
testList.takeFirst();
qDebug() << "m_x: " << m_x << " " << "m_y: " << m_y;
m_series->append(m_x, m_y);
scroll(x, 0);
Chart::~Chart()
main.cpp:
#include "chart.h"
#include <QChartView>
#include <QApplication>
#include <QWidget>
#include <QHBoxLayout>
QT_CHARTS_USE_NAMESPACE
int main(int argc, char *argv[])
QApplication a(argc, argv);
QWidget window;
Chart *chart = new Chart;
//chart->setTitle("Dynamic spline chart");
chart->setAnimationOptions(QChart::AllAnimations);
QChartView chartView(chart);
chartView.setRenderHint(QPainter::Antialiasing);
chartView.resize(window.size());
QHBoxLayout *layout = new QHBoxLayout();
layout->addWidget(&chartView);
window.setLayout(layout);
window.setMinimumSize(810, 400);
window.show();
return a.exec();
任何想法如何解决它?谢谢。
[更新]
我已经解决了这个问题。现在效果很好,但我需要设置轴矩形背景颜色。
截图:
任何想法如何设置轴背景颜色?谢谢。
【问题讨论】:
【参考方案1】:我已使用GitHub 中的QtNetworkMonitor
示例解决了这个问题。问题已解决。
【讨论】:
以上是关于QtCharts setRange 问题的主要内容,如果未能解决你的问题,请参考以下文章