QCustomPlot Discussion and Comments

Adding cursorReturn to overview

Any way to manage cursors on a plot? I tried many odd things, but far from a "neat" solution... I think this is the most needed feature. I can't live with plots without cursors!
Basically, I need to paint a line over a plot area but I don't know how to get the QPainter object related to the plot area that I need in order to use Qt QPainter methods like QPainter::drawLine.
Any advice? Thanks a lot in advance for any reply!

I realized that I choosed the wrong way. It's easy to add a quite trivial cursor in this way:

void MainWindow::mousePress(QMouseEvent* event)
QCustomPlot *customPlot=ui->customPlot;
static QCPItemLine *hCursor, *vCursor;
double x=customPlot->xAxis->pixelToCoord(event->pos().x());
double y=customPlot->yAxis->pixelToCoord(event->pos().y());

if(hCursor) customPlot->removeItem(hCursor);
hCursor = new QCPItemLine(customPlot);
hCursor->start->setCoords(QCPRange::minRange, y);
hCursor->end->setCoords(QCPRange::maxRange, y);

if(vCursor) customPlot->removeItem(vCursor);
vCursor = new QCPItemLine(customPlot);
vCursor->start->setCoords( x, QCPRange::minRange);
vCursor->end->setCoords( x, QCPRange::maxRange);


Of course, we must remember to connect the mousePress signal in the application:
connect(ui->customPlot, SIGNAL(mousePress(QMouseEvent*)), this, SLOT(mousePress(QMouseEvent*)));

I enhanced a little bit the behaviour, now I can manage two cursors:

typedef struct {
    QCPItemLine *hLine;
    QCPItemLine *vLine;
} QCPCursor;

bool cursorEnabled=true;

void ManageCursor(QCustomPlot *customPlot, QCPCursor *cursor, double x, double y, QPen pen)
        if(cursor->hLine) customPlot->removeItem(cursor->hLine);
        cursor->hLine = new QCPItemLine(customPlot);
        cursor->hLine->start->setCoords(QCPRange::minRange, y);
        cursor->hLine->end->setCoords(QCPRange::maxRange, y);

        if(cursor->vLine) customPlot->removeItem(cursor->vLine);
        cursor->vLine = new QCPItemLine(customPlot);
        cursor->vLine->start->setCoords( x, QCPRange::minRange);
        cursor->vLine->end->setCoords( x, QCPRange::maxRange);

void MainWindow::mouseRelease(QMouseEvent* event)
    QCustomPlot *customPlot=ui->customPlot;
    static QCPCursor cursor1, cursor2;
    double x=customPlot->xAxis->pixelToCoord(event->pos().x());
    double y=customPlot->yAxis->pixelToCoord(event->pos().y());

    if(event->button() == Qt::LeftButton)
        ManageCursor(customPlot, &cursor1, x, y, QPen(Qt::black));
        ManageCursor(customPlot, &cursor2, x, y, QPen(Qt::white));



I try your code which works fine.
I want select a cursor wich have already plot by using:
but my item can't be selected by user !

When I call :
I have a good reply but i can't iteract with item.

connect(ui->customPlot, SIGNAL(itemClick(QCPAbstractItem*,QMouseEvent*)), this, SLOT(itemClicked(QCPAbstractItem*)));

void MissionDBView::itemClicked(QCPAbstractItem* plottable)
    qDebug() << "Item selected" << plottable->objectName();

I love your cursor. Exactly what I was looking for. Any ideas on how to make it work over several graphs?

As explained here


The selection of the cursor don't work because the values




are too much big, and the function that measure the distance between the mouse hit and the line fails.

The solution is in the thread linked


Not working, as method addItem() doesn't exist in QCustomPlot

Stefan, you don't need addItem anymore.

Thanks, I see.
It works great.
Thank you very much for your fast reply.

Your code is very cool but there is a little mistake, when there are values in -x and -y the cursors are limited

picture cursors

tracer is easy .


Excuse me lancelot but can you explain me, iI didn't find the solution in the doc :'(

oups, yes it's easy :
QCPRange::minRange; ;))))