MouseMoveEvent 停止被调用

时间:2021-08-01 23:17:13

标签: qt qt5 qtquick2 qcustomplot

我在 QQuickPaintedItem(SinePlot 类)上使用 QCustomPlot(plot_ 对象),所以我可以在 QML 中使用它。在 mousePressEvent 中我收集初始点,在 mouseMoveEvent 中我进行计算以添加新点并更新光标点:


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();
}

我也尝试使用 pos()localPos() 但它没有任何区别,这是我得到的:

enter image description here

如您所见,mouseMoveEvent 在一段时间后(释放之前)停止被调用,并且移动光标不会调用它。

这是最小的可重现示例:

#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

就我而言,我得到了“鼠标移动” ~10 次。

0 个答案:

没有答案
相关问题