Uploaded image for project: 'Qt'
  1. Qt
  2. QTBUG-97044

QGraphicsScene: opening a context menu on another item does not send a release on a potential item pressed beforehand

    XMLWordPrintable

Details

    • Bug
    • Resolution: Unresolved
    • P3: Somewhat important
    • None
    • 5.15, 6.x
    • Widgets: GraphicsView
    • Arch Linux, tested with both Qt 5.15 and Qt 6 but I guess the same issue would exist on other systems
    • Linux/X11

    Description

      The problem is :

      If I :

      • click on item A with the left mouse button,
      • while keeping my LMB mouse pressed, move over another item B,
      • do a right-click which triggers a context menu from B,
      • release my LMB over the context menu

      Then item A never gets the "release" for its original LMB press, which breaks the expected state machine.

      I tried forcing a mousegrab or focus on itemA but this does not prevent the context menu event in itemB.
      Most interestingly, it is still A which gets a right mouse press event even when the cursor is over B and the context menu is B's.

      Here's a repro (wanted to attach it but Jira complains about some missing tokens issue ?)

      #include <QApplication>
      #include <QGraphicsScene>
      #include <QGraphicsView>
      #include <QGraphicsItem>
      #include <QGraphicsSceneMouseEvent>
      #include <QDebug>
      #include <QMenu>
      
      class itemA : public QGraphicsItem
      {
      public:
        itemA()
        {
          setAcceptedMouseButtons(Qt::LeftButton | Qt::RightButton);
        }
      
        void mousePressEvent(QGraphicsSceneMouseEvent* ev) override
        {
          qDebug() << "A press";
          ev->accept();
        }
      
        void mouseReleaseEvent(QGraphicsSceneMouseEvent* ev) override
        {
          qDebug() << "A release";
          ev->accept();
        }
      
        QRectF boundingRect() const override
        {
          return {0, 0, 40, 40};
        }
      
        void paint(QPainter* painter, const QStyleOptionGraphicsItem* option, QWidget* widget) override
        {
          painter->fillRect(boundingRect(), Qt::blue);
        }
      };
      
      class itemB : public QGraphicsItem
      {
      public:
        itemB()
        {
          setAcceptedMouseButtons(Qt::LeftButton | Qt::RightButton);
        }
      
        void contextMenuEvent(QGraphicsSceneContextMenuEvent* ev) override
        {
          qDebug() << "B contextmenu";
          QMenu m;
          m.addAction("Foo");
          m.addAction("Bar");
          m.addAction("Baz");
          m.exec(ev->screenPos());
          ev->accept();
        }
      
        QRectF boundingRect() const override
        {
          return {0, 0, 40, 40};
        }
        void paint(QPainter* painter, const QStyleOptionGraphicsItem* option, QWidget* widget) override
        {
          painter->fillRect(boundingRect(), Qt::red);
        }
      };
      
      int main(int argc, char *argv[])
      {
        QApplication a(argc, argv);
      
        QGraphicsScene sc;
        auto ita = new itemA;
        auto itb = new itemB;
        ita->setPos(0, 0);
        itb->setPos(100, 0);
        sc.addItem(ita);
        sc.addItem(itb);
      
        QGraphicsView v{&sc};
        v.show();
        return a.exec();
      }
      

      Attachments

        No reviews matched the request. Check your Options in the drop-down menu of this sections header.

        Activity

          People

            qt.team.quick.subscriptions Qt Quick and Widgets Team
            jcelerier Jean-Michaƫl Celerier
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

            Dates

              Created:
              Updated:

              Gerrit Reviews

                There are no open Gerrit changes