Ejemplo de línea de zoom
El ejemplo muestra cómo crear tu propio efecto de zoom personalizado.
El ejemplo muestra cómo crear tu propio efecto de zoom personalizado con « QRubberBand » utilizando el ratón y cómo utilizar gestos táctiles para desplazarse y hacer zoom.


Ejecución del ejemplo
Puedes ejecutar el ejemplo desde:
- Qt Creator
Abre el modo « Welcome » y selecciona el ejemplo en Examples. Para obtener más información, consulta Qt Creator: Tutorial: Compilar y ejecutar.
- Qt Extension for Visual Studio Code
Ejecute el comando « Qt: Open Qt examples » desde Command Palette y seleccione el ejemplo de la lista. Para obtener más información, consulte Qt Extension for Visual Studio Code: Tutorial: Compilar y ejecutar.
Personalización de los efectos de zoom
En primer lugar, creemos una serie de líneas con algunos datos de ejemplo.
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;
}A continuación, crearemos una vista de gráfico personalizada derivada de `QChartView`:
class ChartView : public QChartViewSobrescribimos el manejo de eventos del ratón y del teclado
protected:
bool viewportEvent(QEvent *event);
void mousePressEvent(QMouseEvent *event);
void mouseMoveEvent(QMouseEvent *event);
void mouseReleaseEvent(QMouseEvent *event);
void keyPressEvent(QKeyEvent *event);A continuación, implementamos una lógica personalizada para los eventos del ratón y del teclado. Por ejemplo, al pulsar la tecla «+» se ampliará la imagen y al pulsar la tecla «-» se reducirá.
void ChartView::keyPressEvent(QKeyEvent *event)
{
switch (event->key()) {
case Qt::Key_Plus:
chart()->zoomIn();
break;
case Qt::Key_Minus:
chart()->zoomOut();
break;También creamos nuestro propio QChart:
class Chart : public QChartDonde podemos gestionar los gestos:
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;
}Ten en cuenta que tendrás que llamar a `grabGesture()` tanto en ` QMainWindow ` como en `QChart`.
© 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.