QtCharts 添加自定义坐标轴

QtCharts add custom axis

我需要在 QtCharts 上有自定义轴。但是,这些行会导致崩溃。

chart->addAxis(axisX, Qt::AlignCenter);
chart->addAxis(axisY, Qt::AlignCenter);

拜托,您能否提供示例如何自定义轴?目的是要有一些指定的范围和轴标题。

QtCharts::QValueAxis *axisX = new QtCharts::QValueAxis();
axisX->setRange(0, 3);
axisX->setTitleText("x");
QtCharts::QValueAxis *axisY = new QtCharts::QValueAxis();
axisY->setRange(0, 3);
axisY->setTitleText("y");

QtCharts::QScatterSeries *series0 = new QtCharts::QScatterSeries();
for (uint i=0; i<size; i++)
    series0->append(vector4.at(i), vector6.at(i));

series0->setName("scatter1");
series0->setMarkerShape(QtCharts::QScatterSeries::MarkerShapeRectangle);
series0->setMarkerSize(10.0);

QtCharts::QChart *chart = new QtCharts::QChart();
chart->addSeries(series0);
chart->createDefaultAxes();
//chart->addAxis(axisX, Qt::AlignCenter);   CRASH
//chart->addAxis(axisY, Qt::AlignCenter);

QtCharts::QChartView *chartView = new QtCharts::QChartView();
chartView->setChart(chart);
chartView->show();

提前致谢

一段时间后我找到了解决方案。它可以帮助某人。

chart->createDefaultAxes();
chart->axes(Qt::Horizontal).back()->setRange(-35, 135);
chart->axes(Qt::Vertical).back()->setRange(-35, 50);
chart->axes(Qt::Horizontal).back()->setTitleText("axis x [mm]");
chart->axes(Qt::Vertical).back()->setTitleText("axis y [mm]");

试试这个:

  chart->addAxis(axisX, Qt::AlignBottom);
  chart->addAxis(axisY, Qt::AlignLeft);

图表中心不是轴的有效位置,我想。