Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QChartView and Callout
QtWS25 Last Chance

QChartView and Callout

Scheduled Pinned Locked Moved Unsolved General and Desktop
3 Posts 2 Posters 593 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    Stefano84
    wrote on last edited by
    #1

    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.

    jsulmJ 1 Reply Last reply
    0
    • S Stefano84

      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.

      jsulmJ Offline
      jsulmJ Offline
      jsulm
      Lifetime Qt Champion
      wrote on last edited by
      #2

      @Stefano84 said in QChartView and Callout:

      How can I solve the problem?

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

      https://forum.qt.io/topic/113070/qt-code-of-conduct

      S 1 Reply Last reply
      2
      • jsulmJ jsulm

        @Stefano84 said in QChartView and Callout:

        How can I solve the problem?

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

        S Offline
        S Offline
        Stefano84
        wrote on last edited by
        #3

        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);
        }
        
        1 Reply Last reply
        0

        • Login

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved