QCustomPlot Discussion and Comments

Cursor change problemsReturn to overview

Dear users,

I have implemented some draggable items based on the various items available in QCustomPlot. That is pretty easy to do by a simple reimplementation of the mouseMoveEvent method. Here is an example of code

void InfLineItem::mouseMoveEvent(QMouseEvent *event, const QPointF &startPos) {
    Q_UNUSED(startPos);
    double pos = (orientation() == Qt::Horizontal) ?
                 clipAxisRect()->axis(QCPAxis::atLeft)->pixelToCoord(event->pos().y()) :
                 clipAxisRect()->axis(QCPAxis::atBottom)->pixelToCoord(event->pos().x());
    setPosition(pos);  // custom method to set the position of the item
    layer()->replot();
}

To inform the user that the item is draggable when he clicks on it (and maintains the pressure), I would like to change the mouse cursor. This is also pretty easy to do by a simple reimplementation of the mousePressEvent and mouseReleaseEvent methods. Here is a simple example of code

void InfLineItem::mousePressEvent(QMouseEvent *event, const QVariant &details) {
    Q_UNUSED(event);
    Q_UNUSED(details);
    QCursor cursor = (orientation()==Qt::Horizontal) ? Qt::SizeVerCursor : Qt::SizeHorCursor;
    parentPlot()->setCursor(cursor);
}

void InfLineItem::mouseReleaseEvent(QMouseEvent *event, const QPointF &startPos) {
    Q_UNUSED(event);
    Q_UNUSED(startPos);
    parentPlot()->setCursor(Qt::ArrowCursor);
}

The problem is that the cursor changes automatically to the default cursor during the dragging of the item as soon as the mouse do not hover it anymore and then changes to the correct cursor when the mouse goes back on the item. In other words, there is a constant switch from one cursor to the other depending on the location of the mouse, and I clearly do not understand why this happens. How do I solve this problem? Is there a way to force the cursor to keep a specific shape as long as a mouse button is pressed? I tried various things, but nothing worked.

There may be something wrong with me way of doing things, but I do not see what. I hope someone could help me.

Best regards,

Vincent

Dear Users,

The code mentionned above is fully functional. I had an ugly bug elsewhere in my program that explained the strange behaviour.

So, problem solved!

Best regards,

Vincent