Hello. I am learning C++ and working with QCustomPlot. I have read this:
https://www.qcustomplot.com/index.php/support/forum/1887
and thought its quite interesting. Unfortunately the post above does not contain complete solution and does not show how to actually override the require methods.
In my mainwindow.cpp I have setup the very basic example:
// generate some data: QVector<double> x(101), y(101); // initialize with entries 0..100 for (int i=0; i<101; ++i) { x[i] = i/50.0 - 1; // x goes from -1 to 1 y[i] = x[i]*x[i]; // let's plot a quadratic function } // create graph and assign data to it: ui->customPlot->addGraph(); ui->customPlot->graph(0)->setData(x, y); // give the axes some labels: ui->customPlot->xAxis->setLabel("x"); ui->customPlot->yAxis->setLabel("y"); // set axes ranges, so we see all data: ui->customPlot->xAxis->setRange(-1, 1); ui->customPlot->yAxis->setRange(0, 1); ui->customPlot->replot(); ui->customPlot->setInteractions(QCP::iRangeDrag | QCP::iRangeZoom | QCP::iSelectPlottables | QCP::iMultiSelect); foreach (QCPAxisRect *rect, ui->customPlot->axisRects()) { rect->setRangeDrag(Qt::Horizontal); rect->setRangeZoom(Qt::Horizontal); }
To override the methods, I have created a custom class named QMyPlot.
See the QMyPlot.h:
#ifndef QMYPLOT_H #define QMYPLOT_H #include "qcustomplot.h" class QMyPlot : public QCPAxisRect { public: QMyPlot(QCustomPlot *parentPlot); void mousePressEvent(QMouseEvent *event, const QVariant &details) override; }; #endif // QMYPLOT_H
See my QMyPlot.cpp:
#include "qmyplot.h" QMyPlot::QMyPlot(QCustomPlot *parentPlot):QCPAxisRect(parentPlot,true) { } void QMyPlot::mousePressEvent(QMouseEvent *event, const QVariant &details) { if (event->button() == Qt::RightButton) { qDebug("Right mouse Press Event\n"); } else { qDebug("Left mouse Press Event\n"); QCPAxisRect::mousePressEvent(event,details); } }
As you can see, I have overridden the mousePressEvent but I do not actually understand how can I use this overridden method?
Could someone suggest what functions do I need to replace in my MainWindow.cpp to get this method to work?