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. Problem getting QGraphicsItem::mousePressEvent to trigger
Qt 6.11 is out! See what's new in the release blog

Problem getting QGraphicsItem::mousePressEvent to trigger

Scheduled Pinned Locked Moved Unsolved General and Desktop
13 Posts 3 Posters 460 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.
  • J Offline
    J Offline
    james b-s
    wrote last edited by
    #4

    The only thing that happens differently between the cases that work is the following

    1. When the timer expires, mousePressEvent is triggered by the QTimer. This case always works.
    2. When someone moves a mouse, the mousePressEvent is triggered by the mouseMoveEvent. After the mousePressEvent is handled, the mouseMoveEvent is passed down to Qt. The wrong behavior occurs befure the mouseMoveEvent is passed down. Its as if QGraphicsScene::mousePressEvent believes that the coordinates in the saved mouse event is not on the QGraphicsObject, even though it is the same coordinates when it works as when it doesn't.
    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote last edited by
      #5

      Hi,

      My first suggestion would be to use a more recent version of Qt to see if you still encounter that issue. The current series is at 6.11 with 6.12 around the corner.

      Do you have some scaling applied ? Did you check if you have the same issue with and without it ?

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      0
      • J Offline
        J Offline
        james b-s
        wrote last edited by james b-s
        #6

        I would love to upgrade Qt to the latest version. I would also love a debugging version of Qt. Unfortunately, both are beyond my control. Believe me, I've tried.

        1 Reply Last reply
        0
        • J Offline
          J Offline
          james b-s
          wrote last edited by
          #7

          I figured out the problem. When the mousePressEvent occurs, I am saving the mouse event by creating a brand new MousePressEvent and copying all the values from the original event via the constructor. When someone moves the mouse, the global position in my private MousePressEvent changes. According to AI, this is a feature.

          Can someone verify if AI is correct? It is matching up with what I am seeing, but I can't find anything in Qt documentation. And it seems rather bizarre and unexpected.

          "A saved QMouseEvent changes its globalPosition when the mouse moves because QMouseEvent objects are dynamically updated or re-dispatched by the window system, or because saving a pointer to the original event object instead of copying its data (QPointF) causes it to reflect subsequent cursor movements.Why It HappensPointers vs. Value Copies: Saving a pointer (QMouseEvent* event) instead of copying the actual position values (QPointF pos = event->globalPosition()) means your variable references the live event structure or next incoming events processed by the event loop.Live Event Reuse: In certain event-handling implementations or custom overrides, the system updates or repurposes event coordinates during continuous tracking.How to Fix ItCopy the Position Immediately: Extract and store the QPointF or QPoint value rather than saving the event pointer.cpp// Bad: saves the pointer, values change as mouse moves
          savedEvent = event;

          // Good: copies the actual screen coordinates
          savedGlobalPos = event->globalPosition();
          Use Value Types: Declare your saved variable as a QPointF (or QPoint) instead of QMouseEvent*."

          1 Reply Last reply
          0
          • Christian EhrlicherC Offline
            Christian EhrlicherC Offline
            Christian Ehrlicher
            Lifetime Qt Champion
            wrote last edited by
            #8

            Since you don't provide code on how you save the QMouseEvent data I would guess you even have luck that your app does not crash when pEvent is really only a pointer to the given QMouseEvent and you use it later in another function called through a QTimer. Noone guarantees that the memory the pointer points to will be deleted.

            Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
            Visit the Qt Academy at https://academy.qt.io/catalog

            J 1 Reply Last reply
            0
            • Christian EhrlicherC Christian Ehrlicher

              Since you don't provide code on how you save the QMouseEvent data I would guess you even have luck that your app does not crash when pEvent is really only a pointer to the given QMouseEvent and you use it later in another function called through a QTimer. Noone guarantees that the memory the pointer points to will be deleted.

              J Offline
              J Offline
              james b-s
              wrote last edited by
              #9

              @Christian-Ehrlicher

              The code is provided in the original post.

              What is happening is that Qt seems to update the global position of existing QMouseEvents when a separate mouse event occurs and Google AI is telling me that that is the way it is supposed to work. I can't find any Qt documentation verifying what AI is telling me, but it matches up exactly with what I am seeing.

              Code copying the mouse event reposted. As I said, it is creating a new, independent, mouse event with the contents of the old mouse event. I have dozens of trace statements in my code, tracing the value of the global position. Once set, the value doesn't change until a new mouse event occurs. Then the value of the existing m_pMousePressEvent is changed without me either creating a new m_pMousePressEvent or passing the a new global position to the existing m_pMousePressEvent.

              The current code uses the newer position() and globalPosition() which did not make a difference.

              m_pMousePressEvent = new QMouseEvent(pEvent->type(),
                                                     pEvent->pos(),
                                                     pEvent->globalPos(),
                                                     pEvent->button(),
                                                     pEvent->buttons(),
                                                     pEvent->modifiers());
              

              basically, I am looking for verification that what AI is telling me and what I am seeing is correct. That that is the way it is supposed to work.

              1 Reply Last reply
              0
              • Christian EhrlicherC Offline
                Christian EhrlicherC Offline
                Christian Ehrlicher
                Lifetime Qt Champion
                wrote last edited by
                #10

                In the original post I don't see where you create pEvent. And when this is a pointer to the original event then my comments are valid and you should be happy that your app don't crash due to an invalid pointerw

                Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                Visit the Qt Academy at https://academy.qt.io/catalog

                1 Reply Last reply
                0
                • J Offline
                  J Offline
                  james b-s
                  wrote last edited by
                  #11

                  Sorry. pEvent is the event passed into mousePressEvent by Qt. You can see how I copy it. m_pMousePressEvent is not a pointer to the original event.

                  The question is now is AI reporting accurate information (which seems to match what I am seeing) in that the globalPosition of existing mouse events will change when other mouse events occur?

                  Christian EhrlicherC 1 Reply Last reply
                  0
                  • J james b-s

                    Sorry. pEvent is the event passed into mousePressEvent by Qt. You can see how I copy it. m_pMousePressEvent is not a pointer to the original event.

                    The question is now is AI reporting accurate information (which seems to match what I am seeing) in that the globalPosition of existing mouse events will change when other mouse events occur?

                    Christian EhrlicherC Offline
                    Christian EhrlicherC Offline
                    Christian Ehrlicher
                    Lifetime Qt Champion
                    wrote last edited by
                    #12

                    @james-b-s said in Problem getting QGraphicsItem::mousePressEvent to trigger:

                    You can see how I copy it.

                    The question is - when do you copy it.

                    Please provide a minimal, compilable example to reproduce your problem instead posting some code snippets where noone knows when and how they are called

                    Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                    Visit the Qt Academy at https://academy.qt.io/catalog

                    1 Reply Last reply
                    0
                    • Christian EhrlicherC Offline
                      Christian EhrlicherC Offline
                      Christian Ehrlicher
                      Lifetime Qt Champion
                      wrote last edited by
                      #13

                      This works fine for me

                      class MyQGraphicsItem : public QGraphicsItem
                      {
                      public:
                          using QGraphicsItem::QGraphicsItem;
                          QRectF boundingRect() const override { return QRectF(0, 0, 50, 50); }
                          void paint(QPainter* painter, const QStyleOptionGraphicsItem* option, QWidget* widget = nullptr) override
                          {
                              painter->fillRect(boundingRect(), Qt::red);
                          }
                      };
                      
                      class MyQGraphicsView : public QGraphicsView
                      {
                      public:
                          using QGraphicsView::QGraphicsView;
                          void mousePressEvent(QMouseEvent *event) override
                          {
                              delete moveEvent;
                              moveEvent = new QMouseEvent(event->type(), event->localPos(), event->globalPos(), event->button(), event->buttons(), event->modifiers());
                              qDebug() << event;
                              QTimer::singleShot(1000, this, &MyQGraphicsView::doSomething);
                          }
                          void doSomething()
                          {
                              qDebug() << moveEvent;
                          }
                          QMouseEvent* moveEvent = nullptr;
                      };
                      
                      int main(int argc, char* argv[])
                      {
                          QApplication app(argc, argv);
                          QGraphicsScene scene;
                          scene.addItem(new MyQGraphicsItem);
                          MyQGraphicsView view(&scene);
                          view.show();
                          return app.exec();
                      }
                      

                      --> same local & global pos even after 1 second timer.

                      Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                      Visit the Qt Academy at https://academy.qt.io/catalog

                      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