- 问题描述
- 解决方案
- 后记
在使用QtCharts模块绘图的过程中,X轴需要显示DateTime,不能使用默认的createDefaultAxes();遂使用了setAxisX,setAxisY方法,但一直报方法被弃用,虽暂时程序可以使用,为了以后程序打算,加之本人对代码具有强迫症,随查看Qt手册,尝试解决办法。
话不多说,直接贴代码
histChart->addAxis(axisX, Qt::AlignBottom);
histChart->addAxis(axisY, Qt::AlignLeft);
series->attachAxis(axisX);
series->attachAxis(axisY);
使用以上四行代码,即可完美解决问题。
后记为了方便阅读本文的作者更好的理解,决定粘贴本文绘图部分代码,供读者参考,希望对您有所帮助。
//头文件定义的内容
QHBoxLayout *chartLayout;
QChart *histChart;
QDateTimeAxis *axisX;
QValueAxis *axisY;
QChartView * histView;
//构造方法初始化的内容
histChart = new QChart();
axisX = new QDateTimeAxis();
axisY = new QValueAxis();
histView = new QChartView();
chartLayout->addWidget(histView);
//series 数据append在另一个类中函数来赋值
QSplineSeries *series = new QSplineSeries();
series->append(QPointF(histTime, histValue));
//绘图函数里的内容,绘图函数需传参数series,
histChart->removeAllSeries();
histChart->legend()->hide();
histChart->setTheme(QChart::ChartThemeLight);
histChart->setTitle("图title");
axisX->setTitleText("日期时间");
axisX->setFormat("yyyy-MM-dd HH:mm:ss");
axisY->setTitleText("Y值");
histChart->addSeries(series);
histChart->addAxis(axisX, Qt::AlignBottom);
histChart->addAxis(axisY, Qt::AlignLeft);
series->attachAxis(axisX);
series->attachAxis(axisY);
histView->setChart(histChart);
最终绘制的图表如下:


![[Qt5.12.3] 使用Chart模块中warning: ‘setAxisX‘ is deprecated, warning: ‘setAxisY‘ is deprecated的解决办法 [Qt5.12.3] 使用Chart模块中warning: ‘setAxisX‘ is deprecated, warning: ‘setAxisY‘ is deprecated的解决办法](http://www.mshxw.com/aiimages/31/1008714.png)
