W
W
WTFAYD2018-03-26 16:12:45
Qt
WTFAYD, 2018-03-26 16:12:45

How to make QwtPlotPicker and QQuickPaintedItem work together?

Hello.
There is a task to implement a program that builds a graph by points in QML. I decided to use Qwt and QQuickPaintedItem for this.

class QmlQwtPlot : public QQuickPaintedItem
{
    Q_OBJECT

public:
    QmlQwtPlot(QQuickItem* parent = 0);
    ~QmlQwtPlot();

    void paint(QPainter *painter) override;

    void mousePressEvent(QMouseEvent *event) override;
    void mouseMoveEvent(QMouseEvent* event) override;
    void mouseReleaseEvent(QMouseEvent* event) override;
    void wheelEvent(QWheelEvent *event) override;

private:
    QwtPlot* qwtPlot;
    QwtPlotPanner* panner;
    bool isDragging;
    QPoint previousPosition;
    QwtPlotPicker* picker;
};

The ability to move the chart with the mouse was implemented through QwtPlotPanner and mouseMoveEvent():
void QmlQwtPlot::mouseMoveEvent(QMouseEvent* event) {
    if (isDragging) {  // if the mouse button has been pressed
        QPoint currentPosition = event->pos();
        QPoint diff = currentPosition - previousPosition;
        emit panner->panned(diff.x(),diff.y());
        previousPosition = event->pos();
        update();
    }
}

However, there is no way to use QwtPlotPicker to display the cursor (coordinates near the cursor and a cross line on the graph). This class works great with QWidget:
/* QWidget's child constructor (the same is in QQuickPaintedItem's one) */
PlottingWidget::PlottingWidget(QWidget *parent)
    : QWidget(parent)
{
    // ...
    d_picker = new QwtPlotPicker(
        QwtPlot::xBottom, QwtPlot::yLeft,
        QwtPlotPicker::CrossRubberBand,
        QwtPicker::AlwaysOn,
        plot->canvas() 
    );

    d_picker->setRubberBandPen( QColor( Qt::red ) );
    d_picker->setTrackerPen( QColor( Qt::black ) );
    d_picker->setStateMachine( new QwtPickerTrackerMachine() );

    // ...
}

Please tell me how to be in this situation? Write your own class?

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question