Move in qt chart

2k views Asked by At

I have created Spline chart from points.

enter image description here

Now I would like to move on this chart using arrows on keyboard (left and right) and print all points (x,y) while moving.

'move' - I mean, place at the beginning of chart line 'bigger dot than chart line width' and use keyboard (<-,->) to move this dot.

How to do this?

1

There are 1 answers

0
Dinendal On

You can draw a point on the graph with a QGraphicsEllipseItem : http://doc.qt.io/qt-4.8/qgraphicsellipseitem.html

Pass you chart item as a parameter to the QGraphicsEllipseItem.

QGraphicsEllipseItem *ellipseItem = new QGraphicsEllipseItem(chart);

Then create a QChartView :

QChartView *chartView = new QChartView(chart);

where you can re-implement the function to catch the keys pressed :

void View::keyPressEvent(QKeyEvent *event)
{
    switch (event->key())
    {
    case Qt::Key_Left:
        ...
        break;
    case Qt::Key_Right:
        ...
        break;
    default:
        QGraphicsView::keyPressEvent(event);
        break;
    }
}

When those keys are pressed you can go from a point to the next one. To retrieve a point position, use your spline series functions inherited from QXYSeries (http://doc.qt.io/qt-5/qxyseries.html) :

QPointF point = splineLine->at(index);

Transform the position in chart to the position in screen :

QPointF pointPos =  chart->mapToPosition(point);

Then position your ellipseItem :

ellipseItem->setPos(pointPos);

To display the x and y values same thing but use a QGraphicsSimpleTextItem instead of QGraphicsEllipseItem. You might have to adjust their position manually so they are not displayed on top of each other, for example:

textItemX->setPos(pointPos.x() - 5, pointPos.y() +10);

And set the text:

QString textX = QString("x: %1").arg(pointPos.x());
textItemX->setText(textX);