Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt
  • Unsolved
Collapse
qtws26_logo

The Qt Event of the Year: Qt World Summit 2026

Save Your Spot >
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Cant get "mouseMoveEvent(QGraphicsSceneMouseEvent *mouseEvent)" to work.
Qt 6.12 LTS is out! See what's new in the release blog

Cant get "mouseMoveEvent(QGraphicsSceneMouseEvent *mouseEvent)" to work.

Scheduled Pinned Locked Moved Unsolved General and Desktop
11 Posts 4 Posters 3.1k Views 2 Watching
  • 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.
  • ademmlerA Offline
    ademmlerA Offline
    ademmler
    wrote on last edited by
    #1

    Hi there,

    in a bigger project I try too create a dialog for editing whitepoint and greybalance.
    In order to trace and detect my issue I have extracted the minimal code.

    What do I have: A dialog with 2 GroupBoxes. Each has a GraphicsView and his own scene.
    After painting a background I add an ellipse-item and set:

    QGraphicsEllipseItem *ellipse = new QGraphicsEllipseItem (250 -5, 250 - 5, 10, 10);    
    ellipse->setFlags(QGraphicsItem::ItemIsMovable | QGraphicsItem::ItemIsSelectable | QGraphicsItem::ItemIsFocusable | QGraphicsItem::ItemSendsScenePositionChanges | QGraphicsItem::ItemSendsGeometryChanges);
    scene->addItem(ellipse);
    

    I do this, because I aim to grab and move this circle - which works.
    Now I try to react on the movement and position changes. In my minimal.h .

    void mouseMoveEvent(QGraphicsSceneMouseEvent *mouseEvent);
    

    In my minimal.cpp

    void WpEditor::mouseMoveEvent(QGraphicsSceneMouseEvent *event)
    {
        mPoint = event->pos();
        qDebug() << "Mouse move " << mPoint;
        QGraphicsScene::mouseMoveEvent(event);
    }
    

    My issue: Nothing happens. What I am doing wrong here.

    thx and regards

    PS: If you need the full example pls let me know where to send it. I still can't upload here.

    JonBJ 1 Reply Last reply
    0
    • ademmlerA ademmler

      Hi there,

      in a bigger project I try too create a dialog for editing whitepoint and greybalance.
      In order to trace and detect my issue I have extracted the minimal code.

      What do I have: A dialog with 2 GroupBoxes. Each has a GraphicsView and his own scene.
      After painting a background I add an ellipse-item and set:

      QGraphicsEllipseItem *ellipse = new QGraphicsEllipseItem (250 -5, 250 - 5, 10, 10);    
      ellipse->setFlags(QGraphicsItem::ItemIsMovable | QGraphicsItem::ItemIsSelectable | QGraphicsItem::ItemIsFocusable | QGraphicsItem::ItemSendsScenePositionChanges | QGraphicsItem::ItemSendsGeometryChanges);
      scene->addItem(ellipse);
      

      I do this, because I aim to grab and move this circle - which works.
      Now I try to react on the movement and position changes. In my minimal.h .

      void mouseMoveEvent(QGraphicsSceneMouseEvent *mouseEvent);
      

      In my minimal.cpp

      void WpEditor::mouseMoveEvent(QGraphicsSceneMouseEvent *event)
      {
          mPoint = event->pos();
          qDebug() << "Mouse move " << mPoint;
          QGraphicsScene::mouseMoveEvent(event);
      }
      

      My issue: Nothing happens. What I am doing wrong here.

      thx and regards

      PS: If you need the full example pls let me know where to send it. I still can't upload here.

      JonBJ Offline
      JonBJ Offline
      JonB
      wrote on last edited by JonB
      #2

      @ademmler
      You mean your method is not hit? Start by appending override in the .h file to be sure.

      WpEditor is indeed a QGraphicsScene and not a QGraphicsView, right?

      In the event I think you will want to use event->scenePos(); rather than event->pos();.

      ademmlerA 1 Reply Last reply
      0
      • M Offline
        M Offline
        mchinand
        wrote on last edited by
        #3

        It might be easier to sub-class QGraphicsObject and then make connections to its x/yChanged() signals.

        1 Reply Last reply
        0
        • M Offline
          M Offline
          mchinand
          wrote on last edited by
          #4

          @ademmler said in Cant get "mouseMoveEvent(QGraphicsSceneMouseEvent *mouseEvent)" to work.:

          void WpEditor::mouseMoveEvent(QGraphicsSceneMouseEvent *event)

          What is your WpEditor class a sub-class of?

          1 Reply Last reply
          1
          • JonBJ JonB

            @ademmler
            You mean your method is not hit? Start by appending override in the .h file to be sure.

            WpEditor is indeed a QGraphicsScene and not a QGraphicsView, right?

            In the event I think you will want to use event->scenePos(); rather than event->pos();.

            ademmlerA Offline
            ademmlerA Offline
            ademmler
            wrote on last edited by
            #5

            @JonB said in Cant get "mouseMoveEvent(QGraphicsSceneMouseEvent *mouseEvent)" to work.:

            @ademmler

            Dear JonB - thx for your feedback.

            You mean your method is not hit? Start by appending override in the .h file to be sure.

            When I do so I get:
            error: non-virtual member function marked 'override' hides virtual member function
            Honestly I have never understood when I can override a QT function and when I can't.

            WpEditoris indeed aQGraphicsSceneand not aQGraphicsView`, right?

            It is a QGrahicsview (in an UI file) which I attache a scene to. In the constructor I do:
            ... some stuff
            sceneWp = new QGraphicsScene(0, 0, 500, 500);
            viewWp->setScene(sceneWp);
            ... some more stuff

            In the event I think you will want to use event->scenePos(); rather than event->pos();.

            Yes this is correct - my fault !

            jsulmJ JonBJ 2 Replies Last reply
            0
            • ademmlerA ademmler

              @JonB said in Cant get "mouseMoveEvent(QGraphicsSceneMouseEvent *mouseEvent)" to work.:

              @ademmler

              Dear JonB - thx for your feedback.

              You mean your method is not hit? Start by appending override in the .h file to be sure.

              When I do so I get:
              error: non-virtual member function marked 'override' hides virtual member function
              Honestly I have never understood when I can override a QT function and when I can't.

              WpEditoris indeed aQGraphicsSceneand not aQGraphicsView`, right?

              It is a QGrahicsview (in an UI file) which I attache a scene to. In the constructor I do:
              ... some stuff
              sceneWp = new QGraphicsScene(0, 0, 500, 500);
              viewWp->setScene(sceneWp);
              ... some more stuff

              In the event I think you will want to use event->scenePos(); rather than event->pos();.

              Yes this is correct - my fault !

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

              @ademmler said in Cant get "mouseMoveEvent(QGraphicsSceneMouseEvent *mouseEvent)" to work.:

              error: non-virtual member function marked 'override' hides virtual member function

              Show the code. You're not correctly overriding.

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

              1 Reply Last reply
              0
              • ademmlerA ademmler

                @JonB said in Cant get "mouseMoveEvent(QGraphicsSceneMouseEvent *mouseEvent)" to work.:

                @ademmler

                Dear JonB - thx for your feedback.

                You mean your method is not hit? Start by appending override in the .h file to be sure.

                When I do so I get:
                error: non-virtual member function marked 'override' hides virtual member function
                Honestly I have never understood when I can override a QT function and when I can't.

                WpEditoris indeed aQGraphicsSceneand not aQGraphicsView`, right?

                It is a QGrahicsview (in an UI file) which I attache a scene to. In the constructor I do:
                ... some stuff
                sceneWp = new QGraphicsScene(0, 0, 500, 500);
                viewWp->setScene(sceneWp);
                ... some more stuff

                In the event I think you will want to use event->scenePos(); rather than event->pos();.

                Yes this is correct - my fault !

                JonBJ Offline
                JonBJ Offline
                JonB
                wrote on last edited by
                #7

                @ademmler said in Cant get "mouseMoveEvent(QGraphicsSceneMouseEvent *mouseEvent)" to work.:

                error: non-virtual member function marked 'override' hides virtual member function
                Honestly I have never understood when I can override a QT function and when I can't.

                This is totally a C++ issue, nothing special about Qt. And it is really easy to tell:

                • If a method is marked virtual you can override it. And Qt docs will always show virtual for such methods.
                • If a method is not marked virtual you cannot override it.

                "Simplez" :) override is not mandatory, but I suggest you should always put it in if you are intending to override a method. Because you get an error then if the method, or its signature, are not virtual/overridable, and you will know. And in this case the error message tells you exactly that.

                So you must either sub-class your QGraphicsScene and override its void mouseMoveEvent(QGraphicsSceneMouseEvent *mouseEvent) (which I have never tried) or you need to override the QGraphicsView::mouseMoveEvent(QMouseEvent *event) in your WpEditor. In this case, confirm that you will keep the left mouse pressed down while you do drag-move, else you will need to call QGraphicsView::setMouseTracking(true) .

                Meanwhile, I noticed your original QGraphicsScene::mouseMoveEvent(event); in WpEditor::mouseMoveEvent(), your attempt to call the base class implementation. I took you at your word, but you need QGraphicsView::mouseMoveEvent(event); instead!

                ademmlerA 1 Reply Last reply
                2
                • JonBJ JonB

                  @ademmler said in Cant get "mouseMoveEvent(QGraphicsSceneMouseEvent *mouseEvent)" to work.:

                  error: non-virtual member function marked 'override' hides virtual member function
                  Honestly I have never understood when I can override a QT function and when I can't.

                  This is totally a C++ issue, nothing special about Qt. And it is really easy to tell:

                  • If a method is marked virtual you can override it. And Qt docs will always show virtual for such methods.
                  • If a method is not marked virtual you cannot override it.

                  "Simplez" :) override is not mandatory, but I suggest you should always put it in if you are intending to override a method. Because you get an error then if the method, or its signature, are not virtual/overridable, and you will know. And in this case the error message tells you exactly that.

                  So you must either sub-class your QGraphicsScene and override its void mouseMoveEvent(QGraphicsSceneMouseEvent *mouseEvent) (which I have never tried) or you need to override the QGraphicsView::mouseMoveEvent(QMouseEvent *event) in your WpEditor. In this case, confirm that you will keep the left mouse pressed down while you do drag-move, else you will need to call QGraphicsView::setMouseTracking(true) .

                  Meanwhile, I noticed your original QGraphicsScene::mouseMoveEvent(event); in WpEditor::mouseMoveEvent(), your attempt to call the base class implementation. I took you at your word, but you need QGraphicsView::mouseMoveEvent(event); instead!

                  ademmlerA Offline
                  ademmlerA Offline
                  ademmler
                  wrote on last edited by ademmler
                  #8

                  @JonB thx again JonB.

                  Either I subclass QGraphicsScene - as you wrote before or using an event filter might help. I found this thread about it.
                  https://stackoverflow.com/questions/51151806/use-a-qgraphicsscenemouseevent-in-a-class-that-inherits-from-qgraphicsview

                  bool DisplayImage::eventFilter(QObject *watched, QEvent *event)
                  {
                      if(watched == scene){
                          // press event
                          QGraphicsSceneMouseEvent *mouseSceneEvent;
                          if(event->type() == QEvent::GraphicsSceneMousePress){
                              mouseSceneEvent = static_cast<QGraphicsSceneMouseEvent *>(event);
                              qDebug()<<mouseSceneEvent->scenePos()<<mouseSceneEvent->lastScenePos();
                             // your logic here
                          }
                          // move event
                          else if (event->type() == QEvent::GraphicsSceneMouseMove) {
                              mouseSceneEvent = static_cast<QGraphicsSceneMouseEvent *>(event);
                              qDebug()<<mouseSceneEvent->scenePos()<<mouseSceneEvent->lastScenePos();
                              // your logic here
                          }
                          // release event
                          else if (event->type() == QEvent::GraphicsSceneMouseRelease) {
                              mouseSceneEvent = static_cast<QGraphicsSceneMouseEvent *>(event);
                              qDebug()<<mouseSceneEvent->scenePos()<<mouseSceneEvent->lastScenePos();
                              // your logic here
                          }
                      }
                      return QGraphicsView::eventFilter(watched, event);
                  }
                  
                  JonBJ 1 Reply Last reply
                  0
                  • ademmlerA ademmler

                    @JonB thx again JonB.

                    Either I subclass QGraphicsScene - as you wrote before or using an event filter might help. I found this thread about it.
                    https://stackoverflow.com/questions/51151806/use-a-qgraphicsscenemouseevent-in-a-class-that-inherits-from-qgraphicsview

                    bool DisplayImage::eventFilter(QObject *watched, QEvent *event)
                    {
                        if(watched == scene){
                            // press event
                            QGraphicsSceneMouseEvent *mouseSceneEvent;
                            if(event->type() == QEvent::GraphicsSceneMousePress){
                                mouseSceneEvent = static_cast<QGraphicsSceneMouseEvent *>(event);
                                qDebug()<<mouseSceneEvent->scenePos()<<mouseSceneEvent->lastScenePos();
                               // your logic here
                            }
                            // move event
                            else if (event->type() == QEvent::GraphicsSceneMouseMove) {
                                mouseSceneEvent = static_cast<QGraphicsSceneMouseEvent *>(event);
                                qDebug()<<mouseSceneEvent->scenePos()<<mouseSceneEvent->lastScenePos();
                                // your logic here
                            }
                            // release event
                            else if (event->type() == QEvent::GraphicsSceneMouseRelease) {
                                mouseSceneEvent = static_cast<QGraphicsSceneMouseEvent *>(event);
                                qDebug()<<mouseSceneEvent->scenePos()<<mouseSceneEvent->lastScenePos();
                                // your logic here
                            }
                        }
                        return QGraphicsView::eventFilter(watched, event);
                    }
                    
                    JonBJ Offline
                    JonBJ Offline
                    JonB
                    wrote on last edited by
                    #9

                    @ademmler
                    Yes, you can always do sub-classing/events via eventFilter() if that is what you want. It can avoid need for sub-classing in some cases, but the code is/may be "messier" then overriding the methods you need. As you please.

                    1 Reply Last reply
                    0
                    • M Offline
                      M Offline
                      mchinand
                      wrote on last edited by
                      #10

                      What are you actually trying to do? I had suggested using a QGraphicsObject and its signals because I thought you were moving around the ellipse you create to change two parameters that affect other things in your application/UI and its position would reflect the current value of those parameters. If this is the case, even if you do this by watching mouse events in the scene, you will want to use changes in the ellipse object's position to make your parameter changes.

                      ademmlerA 1 Reply Last reply
                      1
                      • M mchinand

                        What are you actually trying to do? I had suggested using a QGraphicsObject and its signals because I thought you were moving around the ellipse you create to change two parameters that affect other things in your application/UI and its position would reflect the current value of those parameters. If this is the case, even if you do this by watching mouse events in the scene, you will want to use changes in the ellipse object's position to make your parameter changes.

                        ademmlerA Offline
                        ademmlerA Offline
                        ademmler
                        wrote on last edited by
                        #11

                        @mchinand

                        hi you are right - I want to change parameters according to x/y changes.
                        I will have a look at you solution too! And come back of course.

                        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
                        • Unsolved