Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

QChartView and Callout



  • Hello everybody,

    I'm trying to integrate the "Callout" example (https://doc.qt.io/qt-5/qtcharts-callout-example.html) with the "Zoom Line Example" (https://doc.qt.io/qt-5/qtcharts-zoomlinechart-example.html).

    I managed to draw the tooltip on the graph but I cannot intercept the mouse events, i.e .:

    void Callout::mousePressEvent(QGraphicsSceneMouseEvent *event)
    {
    event->setAccepted(true);
    }

    void Callout::mouseMoveEvent(QGraphicsSceneMouseEvent *event)
    {
    if (event->buttons() & Qt::LeftButton){
    setPos(mapToParent(event->pos() - event->buttonDownPos(Qt::LeftButton)));
    event->setAccepted(true);
    } else {
    event->setAccepted(false);
    }
    }

    This is most likely due to the ChartView class catching events first.
    How can I solve the problem?

    Can anyone help me out?

    Thank you very much.


  • Lifetime Qt Champion

    @Stefano84 said in QChartView and Callout:

    How can I solve the problem?

    Don't set accepted to true, so the event gets propagated further.



  • Hi @jsulm,

    Thanks for your reply.
    I removed the lines but nothing has changed.

    In my opinion it is the QChartView class that does not propagate events and therefore the Callout class does not intercept them.

    Here is the code for the QChartView class.

    Can you help me?
    Thank you very much

    #include "chartview.h"
    #include <QtGui/QMouseEvent>
    
    ChartView::ChartView(QChart *chart, QWidget *parent) :
        QChartView(chart, parent),
        m_isTouching(false)
    {
        setRubberBand(QChartView::RectangleRubberBand);
    }
    
    void ChartView::updateChart()
    {
        chart()->update();
    }
    
    bool ChartView::viewportEvent(QEvent *event)
    {
        if (event->type() == QEvent::TouchBegin) {
            // By default touch events are converted to mouse events. So
            // after this event we will get a mouse event also but we want
            // to handle touch events as gestures only. So we need this safeguard
            // to block mouse events that are actually generated from touch.
            m_isTouching = true;
    
            // Turn off animations when handling gestures they
            // will only slow us down.
            //chart()->setAnimationOptions(QChart::NoAnimation);
        }
        return QChartView::viewportEvent(event);
    }
    
    void ChartView::mousePressEvent(QMouseEvent *event)
    {
        if (m_isTouching)
            return;
    
        QChartView::mousePressEvent(event);
    }
    
    void ChartView::mouseMoveEvent(QMouseEvent *event)
    {
        if (m_isTouching)
            return;
    
        emit SignalChartCoordinates(event->pos());
    
        QChartView::mouseMoveEvent(event);
    }
    
    void ChartView::mouseReleaseEvent(QMouseEvent *event)
    {
        if (m_isTouching)
            m_isTouching = false;
    
        // Because we disabled animations when touch event was detected
        // we must put them back on.
        //chart()->setAnimationOptions(QChart::SeriesAnimations);
    
        QChartView::mouseReleaseEvent(event);
    }
    
    //![1]
    void ChartView::keyPressEvent(QKeyEvent *event)
    {
        switch (event->key()) {
        case Qt::Key_Plus:
            chart()->zoomIn();
            break;
        case Qt::Key_Minus:
            chart()->zoomOut();
            break;
    //![1]
        case Qt::Key_Left:
            chart()->scroll(-10, 0);
            break;
        case Qt::Key_Right:
            chart()->scroll(10, 0);
            break;
        case Qt::Key_Up:
            chart()->scroll(0, 10);
            break;
        case Qt::Key_Down:
            chart()->scroll(0, -10);
            break;
        default:
            QGraphicsView::keyPressEvent(event);
            break;
        }
    }
    
    void ChartView::wheelEvent(QWheelEvent *event)
    {
        qreal factor = event->angleDelta().y() > 0 ? 2.0 : 0.5;
        chart()->zoom(factor);
    
        QPointF c = chart()->plotArea().center();
        QPointF dPos = event->pos();
    
        if (factor == 2.0)
            chart()->scroll(-(c.x() - dPos.x()), (c.y() - dPos.y()));
        else
            chart()->scroll((c.x() - dPos.x()) / 2, -(c.y() - dPos.y()) / 2);
    
        event->accept();
    
        QChartView::wheelEvent(event);
    }
    

Log in to reply