我现在正在融化我的大脑,不明白为什么我的图表根本不更新。我在my applications *.h-File中声明了以下内容:
QT_CHARTS_NAMESPACE::QChart* chart;
QT_CHARTS_NAMESPACE::QLineSeries* series;
QT_CHARTS_NAMESPACE::QChartView* chartView;在我编写的主窗口*.cpp文件的构造函数中
this->series= new QT_CHARTS_NAMESPACE::QLineSeries();
this->chart = new QT_CHARTS_NAMESPACE::QChart();
this->chartView = new QT_CHARTS_NAMESPACE::QChartView(chart);
this->chartView->setRenderHint(QPainter::Antialiasing);
.
.
.
this->series->clear();
this->chart->legend()->hide();
this->setPlotAreaBackgroundVisible(true);
this->series->setVisible(true);
this->chart->addSeries(this->series);
this->chart->createDefaultAxes();现在series是空的,我从Qt文档中知道我的图形应该在每次append()发出它的信号时更新。
在所有这些实例化之后,我的应用程序完成了它的工作。在来自另一个线程的槽中,应该将一些数据附加到序列中:
/*Some other things*/
this->seriesXIncrement++;
QPoint point = QPoint(this->seriesXIncrement,this->parsedReadCommand.toUInt());
this->series->append(point);
qDebug()<<this->series->points().size();
this->chart->createDefaultAxes();
this->chartView->repaint();我已经用qDebug验证了append方法。它的大小正像预期的那样增长,我创建的点也是有效的,但在我的图上什么也没有发生。我不确定createDefaultAxes()是否也是更新轴的方法,但我认为这是一种相对简单和懒惰的方法。
发布于 2021-01-20 15:51:15
我发现,是createDefaultAxes()导致了这个问题。我插入了createDefaultAxes()的两个(x,y)轴实例,如下所示:
QT_CHARTS_NAMESPACE::QValueAxis *axisX = new QT_CHARTS_NAMESPACE::QValueAxis;
axisX->setRange(0, this->series->points().length());
axisX->setTickCount(10);
axisX->setLabelFormat("%d");
chartView->chart()->setAxisX(axisX, series);
QT_CHARTS_NAMESPACE::QValueAxis *axisY = new QT_CHARTS_NAMESPACE::QValueAxis;
axisY->setRange(0, 50);
axisY->setTickCount(1);
axisY->setLabelFormat("%d");
chartView->chart()->setAxisY(axisY, series);这就解决了问题。
https://stackoverflow.com/questions/65777697
复制相似问题