提升为QtCharts::QChartView

真的服了,之前不加QtCharts::都可以,现在不行了

优化代码 1.鼠标出图表区域后不显示坐标 2.显示的坐标不准确,与图表x轴y轴坐标有误差 #ifndef CUSTOMCHARTVIEW_H #define CUSTOMCHARTVIEW_H #include <QtCharts/QChartView> QT_CHARTS_USE_NAMESPACE #include <QLabel> #include <QRubberBand> #include <QValueAxis> class CustomChartView : public QtCharts::QChartView { Q_OBJECT public: explicit CustomChartView(QWidget* parent = nullptr); explicit CustomChartView(QtCharts::QChart* chart, QWidget* parent = nullptr); protected: void mousePressEvent(QMouseEvent* event) override { if (event->button() == Qt::LeftButton) { dragStart = event->pos(); if (!rubberBand) { rubberBand = new QRubberBand(QRubberBand::Rectangle, this); } rubberBand->setGeometry(QRect(dragStart, QSize())); rubberBand->show(); } else if (event->button() == Qt::RightButton) { lastPos = event->pos(); setCursor(Qt::ClosedHandCursor); } QChartView::mousePressEvent(event); } void mouseMoveEvent(QMouseEvent* event) override { updateCoordinateLabel(event->pos()); if (rubberBand && rubberBand->isVisible()) { rubberBand->setGeometry(QRect(dragStart, event->pos()).normalized()); } else if (event->buttons() & Qt::RightButton) { auto dPos = event->pos() - lastPos; chart()->scroll(-dPos.x(), dPos.y()); lastPos = event->pos(); } QChartView::mouseMoveEvent(event); } void mouseReleaseEvent(QMouseEvent* event) override { if (event->button() == Qt::LeftButton && rubberBand && rubberBand->isVisible()) { auto rect = rubberBand->geometry(); rubberBand->hide(); if (rect.width() > 5 && rect.height() > 5) { auto zoomRect = chart()->plotArea().intersected(rect); chart()->zoomIn(zoomRect); } } else if (event->button() == Qt::RightButton) { setCursor(Qt::ArrowCursor); } QChartView::mouseReleaseEvent(event); } void mouseDoubleClickEvent(QMouseEvent* event) override { if (event->button() == Qt::LeftButton) { chart()->zoomReset(); // 双击恢复原始视图 } QChartView::mouseDoubleClickEvent(event); } private: void updateCoordinateLabel(const QPoint& pos) { auto chartPos = chart()->mapToValue(pos); coordLabel->setText(QString("X: %1, Y: %2").arg(chartPos.x()).arg(chartPos.y())); coordLabel->adjustSize(); } QPoint dragStart; QPoint lastPos; QRubberBand* rubberBand; QLabel* coordLabel; }; #endif // CUSTOMCHARTVIEW_H
最新发布
12-13
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值