このページでは

ズームラインの例

この例では、独自のズーム効果を作成する方法を示しています。

このサンプルでは、QRubberBand を使用して、マウスによる独自のズーム効果を作成する方法と、パンやズームのためのタッチジェスチャーの使用方法を示しています。

波形チャートの例を示すスクリーンショット

波形チャートを拡大表示したスクリーンショット

例の動作

このサンプルは、以下の手順で実行できます。

ズーム効果のカスタマイズ

まず、サンプルデータを使用した線グラフシリーズを作成しましょう。

    auto series = new QLineSeries;
    for (int i = 0; i < 500; i++) {
        QPointF p((qreal) i, qSin(M_PI / 50 * i) * 100);
        p.ry() += QRandomGenerator::global()->bounded(20);
        *series << p;
    }

次に、QChartView を継承してカスタムチャートビューを作成します:

class ChartView : public QChartView

マウスおよびキーイベントの処理をオーバーライドします

protected:
    bool viewportEvent(QEvent *event);
    void mousePressEvent(QMouseEvent *event);
    void mouseMoveEvent(QMouseEvent *event);
    void mouseReleaseEvent(QMouseEvent *event);
    void keyPressEvent(QKeyEvent *event);

次に、マウスおよびキーイベントに対するカスタムロジックを実装します。例えば、「+」キーを押すとズームインし、「-」キーを押すとズームアウトするようにします。

void ChartView::keyPressEvent(QKeyEvent *event)
{
    switch (event->key()) {
    case Qt::Key_Plus:
        chart()->zoomIn();
        break;
    case Qt::Key_Minus:
        chart()->zoomOut();
        break;

また、独自の QChart も作成します:

class Chart : public QChart

ここでジェスチャーを処理できます:

bool Chart::sceneEvent(QEvent *event)
{
    if (event->type() == QEvent::Gesture)
        return gestureEvent(static_cast<QGestureEvent *>(event));
    return QChart::event(event);
}

bool Chart::gestureEvent(QGestureEvent *event)
{
    if (QGesture *gesture = event->gesture(Qt::PanGesture)) {
        auto pan = static_cast<QPanGesture *>(gesture);
        QChart::scroll(-(pan->delta().x()), pan->delta().y());
    }

    if (QGesture *gesture = event->gesture(Qt::PinchGesture)) {
        auto pinch = static_cast<QPinchGesture *>(gesture);
        if (pinch->changeFlags() & QPinchGesture::ScaleFactorChanged)
            QChart::zoom(pinch->scaleFactor());
    }

    return true;
}

なお、QMainWindow とQChartの両方でgrabGesture()を呼び出す必要がある点に注意してください。

サンプルプロジェクト @ code.qt.io

© 2026 The Qt Company Ltd. Documentation contributions included herein are the copyrights of their respective owners. The documentation provided herein is licensed under the terms of the GNU Free Documentation License version 1.3 as published by the Free Software Foundation. Qt and respective logos are trademarks of The Qt Company Ltd. in Finland and/or other countries worldwide. All other trademarks are property of their respective owners.