줌 라인 예시
이 예는 사용자 지정 확대/축소 효과를 만드는 방법을 보여줍니다.
이 예에서는 마우스를 사용하여 QRubberBand 로 사용자 지정 줌 효과를 만드는 방법과 터치 제스처를 사용하여 이동 및 확대/축소하는 방법을 보여 줍니다.
예제 실행하기
에서 예제를 실행하려면 Qt Creator에서 Welcome 모드를 열고 Examples 에서 예제를 선택합니다. 자세한 내용은 예제 빌드 및 실행하기를 참조하세요.
확대/축소 효과 사용자 지정하기
먼저 몇 가지 예제 데이터로 라인 시리즈를 만들어 보겠습니다.
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()를 호출해야 합니다.
© 2025 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.