c++qtqtcharts

QtCharts add custom axis


I would need to have custom axis on QtCharts. However, these lines cause crash.

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

Please, can you provide example how to customize the axis? The intent is to have some specified range and axis title.

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();

Thanks in advance


Solution

  • After some time I found the solution. It could help to someone.

    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]");