MouseMoveEvent stops being called

155 Views Asked by At

I'm using QCustomPlot (plot_ object) on QQuickPaintedItem (SinePlot class) so I can use it in QML. In mousePressEvent I collect initial point and in mouseMoveEvent I'm making calculations to add new points and updating cursor point:


void SinePlot::mousePressEvent(QMouseEvent* event)
{
    prevPoint_ = event->globalPos();
}

void SinePlot::mouseMoveEvent(QMouseEvent* event)
{
    QPointF tmp = event->globalPos();
    qreal prop = (prevPoint_.x() - tmp.x()) / width();
    if(prop > 0)
    {
        data_->shiftLeft(prop);
    } else {
        data_->shiftRight(prop);
    }
    plot_->xAxis->setRange(data_->minX, data_->maxX);
    ...
    prevPoint_ = tmp;
    update();
}

I have also trying to use pos() and localPos() but it does not make any difference, here is what I got:

enter image description here

As you can see mouseMoveEvent stops being called after some time(before releasing) and moving cursor does not call it.

Here is minimal reproducible example:

#ifndef SINEPLOT_H
#define SINEPLOT_H

#include "qcustomplot.h"
#include <QtQuick>
#include <QDebug>

class SinePlot : public QQuickPaintedItem
{
    Q_OBJECT
public:
    explicit SinePlot(QQuickItem* parent=nullptr)
    {
            setAcceptedMouseButtons(Qt::AllButtons);
            plot_ = new QCustomPlot();
            plot_->setInteractions(QCP::iRangeDrag);
            plot_->addGraph();
    }
    virtual ~SinePlot()
    {
        delete plot_;
    }
    void paint(QPainter* painter)
    {
        QPicture picture;
        QCPPainter qcpPainter;
        qcpPainter.begin(&picture);
        plot_->toPainter(&qcpPainter, width(), height());
        qcpPainter.end();
        picture.play(painter);
    };

protected:    
    virtual void mousePressEvent(QMouseEvent* event) {};
    virtual void mouseMoveEvent(QMouseEvent* event)
    {
        qDebug() << "mouse move";
    };


private:
    QCustomPlot* plot_;
};

#endif

In my case I got "mouse move" ~10 times.

0

There are 0 best solutions below