首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在QChartView中找不到缩放图形的鼠标滚轮滚动事件

在QChartView中找不到缩放图形的鼠标滚轮滚动事件
EN

Stack Overflow用户
提问于 2018-08-09 02:34:04
回答 2查看 542关注 0票数 1

我在程序窗口中有QChartView。有一些数据数组在图表上正确地显示为QLineSeries (温度与时间的曲线)。我在QChartView上找不到“鼠标轮向上放大”和“鼠标轮向下放大”的鼠标滚轮事件?我需要能够缩放只垂直方向,像一个setRubberBand(QChartView::VerticalRubberBand),但只有通过鼠标滚轮滚动。需要帮助

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2018-08-09 17:28:30

QChartView是一个QWidget,因此您可以使用wheelEvent()方法实现该逻辑:

代码语言:javascript
复制
#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 | HorizontalZoom
    };
    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();
}
票数 0
EN

Stack Overflow用户

发布于 2018-08-09 17:25:34

QChartView继承自提供QGraphicsSceneWheelEventQGraphicsView

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/51753500

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档