I can't find mouse wheel scroll events for zoom graphics in QChartView
Clash Royale CLAN TAG#URR8PPP
I can't find mouse wheel scroll events for zoom graphics in QChartView
I have QChartView on the program window. There are arrays of data that correctly shown on chart as QLineSeries (curves of temperature vs time). I can't find mousewheel events for 'mousewheelup zoom-in' and 'mousewheeldown zoom-out' on QChartView? Need ability to zoom by only vertical direction, like a setRubberBand(QChartView::VerticalRubberBand)
but only by mousewheel scroll. Need help
setRubberBand(QChartView::VerticalRubberBand)
The problem is that I have not found such a signal anywhere. I tried to use chrt = new QChart; chrt->legend()->hide(); ui->vchrt->setChart(chrt); ui->vchrt->setRubberBand(QChartView::VerticalRubberBand); Zoom works, but its need to do zoom-in and zoom-out by mousewheel scroll events. I searched documentation but didn't find that signals.
– Joan Cornela
Aug 9 at 8:53
2 Answers
2
QChartView
is a QWidget
so you could implement that logic using the wheelEvent()
method:
QChartView
QWidget
wheelEvent()
#include <QApplication>
#include <QChartView>
#include <QLineSeries>
#include <random>
QT_CHARTS_USE_NAMESPACE
class ChartView : public QChartView
public:
using QChartView::QChartView;
enum DirectionZoom
NotZoom,
VerticalZoom,
HorizontalZoom,
BothDirectionZoom = VerticalZoom ;
DirectionZoom directionZoom() const
return mDirectionZoom;
void setDirectionZoom(const DirectionZoom &directionZoom)
mDirectionZoom = directionZoom;
protected:
void wheelEvent(QWheelEvent *event)
if(chart() && mDirectionZoom != NotZoom)
const qreal factor = 1.001;
QRectF r = chart()->plotArea();
QPointF c = r.center();
qreal val = std::pow(factor, event->delta());
if(mDirectionZoom & VerticalZoom)
r.setHeight(r.height()*val);
if (mDirectionZoom & HorizontalZoom)
r.setWidth(r.width()*val);
r.moveCenter(c);
chart()->zoomIn(r);
QChartView::wheelEvent(event);
private:
DirectionZoom mDirectionZoom = NotZoom;
;
int main(int argc, char *argv)
QApplication a(argc, argv);
QChart *chart = new QChart();
chart->legend()->hide();
chart->setTitle("Simple line chart example");
std::random_device rd;
std::mt19937 rng(rd());
std::uniform_int_distribution<int> uni(0, 10);
for(size_t i=0; i< 5; i++)
QLineSeries *series = new QLineSeries();
for(size_t j=0; j < 10; j++)
*series << QPointF(j, uni(rng));
chart->addSeries(series);
chart->createDefaultAxes();
ChartView chartView(chart);
chartView.setDirectionZoom(ChartView::VerticalZoom);
chartView.setRenderHint(QPainter::Antialiasing);
chartView.resize(640, 480);
chartView.show();
return a.exec();
Thanks a lot, @eyllanesc !!! Transformed QWidget on Form in ChartView and used your modified class. It works! :) But MousewheelUp action performs zoom-out action instead of zoom-in. Thanks anyway! :)
– Joan Cornela
Aug 9 at 10:40
@JoanCornela only change to
const qreal factor = 1/1.001;
– eyllanesc
Aug 9 at 11:36
const qreal factor = 1/1.001;
thank you very much!
– Joan Cornela
Aug 9 at 12:23
QChartView
inherits from QGraphicsView
which offers QGraphicsSceneWheelEvent
QChartView
QGraphicsView
QGraphicsSceneWheelEvent
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
Everyone needs help, for that is SO. A question where it shows what has been tried usually has better answers, so I recommend you show your solution attempt.
– eyllanesc
Aug 9 at 8:31