QCustomPlot Discussion and Comments

User interaction with QCPItemLineReturn to overview

Hi,
I have some trouble trying to interact with a QCPItemLine. Basically is a cursor for the plot.
I have created it with the following lines

vCursor = new QCPItemLine(ui->qplot);
ui->qplot->addItem(vCursor);
vCursor->setPen(QPen(Qt::black));
vCursor->setSelectedPen(QPen(Qt::red));
vCursor->start->setCoords( 0, QCPRange::minRange);
vCursor->end->setCoords( 0, QCPRange::maxRange);

I enabled the interaction with

ui->qplot->setInteractions(QCP::iSelectItems);

And connected the signal

connect(ui->qplot, SIGNAL(selectionChangedByUser()), this, SLOT(select()));

But the select() slot is not reached when I click on the line.
I tested also the

connect(vCursor, SIGNAL(selectableChanged(bool)), this, SLOT(select(bool)));
connect(vCursor, SIGNAL(selectionChanged(bool)), this, SLOT(select(bool)));

But again slot is not reached.
If I select the item by code

vCursor->setSelected(true);

The cursor become selected (red) and the slot executed.

Any help?
Thanks in advance for any suggestion ^_^

Ciao

After a little bit of debugging (thanks to the open source) I discovered the problem.

Basically it is the setting of the start and end point

vCursor->start->setCoords( 0, QCPRange::minRange);
vCursor->end->setCoords( 0, QCPRange::maxRange);

Write in this way the Y coordinates becomes too much high and the system when evaluating if the mouse hit can't resolve the distance function.

I resolved using the axis values

vCursor[i]->start->setCoords( 0, ui->qplot->yAxis->range().lower);
vCursor[i]->end->setCoords( 0, ui->qplot->yAxis->range().upper);

Of course is necessary also to update these value when dragging the axis

connect(ui->qplot->yAxis, SIGNAL(rangeChanged(QCPRange)), this, SLOT(updateCursorLenghtAfterAxisChange(QCPRange)));

....

void WScope::updateCursorLenghtAfterAxisChange(QCPRange)
{
    int i;
    for (i = 0; i < CURSOR_NUMBER; i++)
    {
        QPointF start = vCursor[i]->start->coords();
        vCursor[i]->start->setCoords(start.x(), ui->qplot->yAxis->range().lower);
        vCursor[i]->end->setCoords(start.x(), ui->qplot->yAxis->range().upper);
    }
}

And now it works


If you want the line to always span over the entire axis rect, I recommend just switching the y position type to ptAxisRectRatio and setting the y coordinates of the position to 0 and 1. This way you don't have to manually update the positions all the time.
See the documentation of QCPItemPosition::setTypeY and QCPItemPosition::PositionType

Alternatively you could use QCPItemStraightLine instead of QCPItemLine. A straight line is defined by a base point coordinate and a direction vector, and spans to "infinity" in both directions.

Thank you very much DerManu

I modified the code using the QCPItemPosition::ptAxisRectRatio and it works great without the need to update the positions.

vCursor[i]->start->setTypeY(QCPItemPosition::ptAxisRectRatio);
vCursor[i]->start->setCoords(x, 0);
vCursor[i]->end->setTypeY(QCPItemPosition::ptAxisRectRatio);
vCursor[i]->end->setCoords(x, 1);

^_^