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. Conflict between 2 context menus
QtWS25 Last Chance

Conflict between 2 context menus

Scheduled Pinned Locked Moved Solved General and Desktop
12 Posts 4 Posters 2.4k 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.
  • D Offline
    D Offline
    DMIII
    wrote on last edited by DMIII
    #1

    Hi guys,
    I have a QGraphicsView observing a QGraphicsScene, and I have implemented a custom context menu on my QGraphicsView (by calling setContextMenuPolicy(Qt::CustomContextMenu)).

    Then I have added a simple QWidget to my scene, containing a QPushButton, and I want to implement another custom context menu on this button. But when I set the QPushButton context menu policy to Qt::CustomContextMenu and connect a slot to the customContextMenuRequested() signal, right clicking on the button opens the QGraphicsView context menu (i.e. the button context menu is covered by the QGraphicsView context menu).

    Is there any way to avoid this overlapping ?
    Thank you for your help.

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi,

      Can you provide a minimal sample code that shows that behaviour ?

      By the way:

      • What version of Qt are you using ?
      • On what platform ?

      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
      2
      • dheerendraD Offline
        dheerendraD Offline
        dheerendra
        Qt Champions 2022
        wrote on last edited by
        #3

        Yes, Here is GraphicsView consumes the events and hence you are seeing the behaviour. Even though you are placing the widget, for the GraphicsView it is item only. Only option is to customise the scene class and write your own event handler.

        Dheerendra
        @Community Service
        Certified Qt Specialist
        http://www.pthinks.com

        1 Reply Last reply
        1
        • D Offline
          D Offline
          DMIII
          wrote on last edited by
          #4

          @SGaist The code is quite big and it involves several classes, but I will try to provide a sample code asap. I am using Qt 4.8 on Windows .
          @dheerendra Which event handlers are you talking about ? I have tried an alternative solution which is using the underMouse method on my widget and disabling the GraphicsView context menu if the right click has been done on my widget. I don't know if this is a correct solution but at the moment I couldn't find any other way to do this.

          1 Reply Last reply
          0
          • SGaistS Offline
            SGaistS Offline
            SGaist
            Lifetime Qt Champion
            wrote on last edited by
            #5

            One possibility to think about: use an event filter. Then check for QContextMenuEvent, if the underlying element is your proxied widget, then forward the event to it, otherwise let it be handled by the scene.

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

            D 1 Reply Last reply
            1
            • SGaistS SGaist

              One possibility to think about: use an event filter. Then check for QContextMenuEvent, if the underlying element is your proxied widget, then forward the event to it, otherwise let it be handled by the scene.

              D Offline
              D Offline
              DMIII
              wrote on last edited by
              #6

              @SGaist I went through all the docs of event filters (http://doc.qt.io/archives/qt-4.8/eventsandfilters.html and http://doc.qt.io/archives/qt-4.8/qobject.html#installEventFilter) but in the line
              monitoredObj->installEventFilter(filterObj); what is the monitored object, the QGraphicsScene or the QGraphicsView ? And should my widget be the filter object ?

              1 Reply Last reply
              0
              • SGaistS Offline
                SGaistS Offline
                SGaist
                Lifetime Qt Champion
                wrote on last edited by
                #7

                That depends on you, you can have a separate object that does the filtering.

                As for the scene or the view, the scene would make more sense it's it that contains all the items.

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

                D 1 Reply Last reply
                1
                • SGaistS SGaist

                  That depends on you, you can have a separate object that does the filtering.

                  As for the scene or the view, the scene would make more sense it's it that contains all the items.

                  D Offline
                  D Offline
                  DMIII
                  wrote on last edited by
                  #8

                  @SGaist I have installed an event filter on the scene, but it turns out that QContextMenuEvent is never called, instead QEvent::GraphicsSceneMousePress and QEvent::GraphicsSceneMouseReleased are called when I click on the widget. In addition, in the function bool FilterObject::eventFilter(QObject *object, QEvent *event), object is always my QGraphicsScene, thus from here I don't know how to forward events to my widget.
                  However I managed to solve this with a workaround : I open the QGraphicsView ( QMenu::exec() ) only when myWidget->underMouse() returns false, and then I reimplement mousePressEvent and mouseReleaseEvent to deal with the right click.

                  1 Reply Last reply
                  1
                  • SGaistS Offline
                    SGaistS Offline
                    SGaist
                    Lifetime Qt Champion
                    wrote on last edited by
                    #9

                    Do you mean in the slot connected to customContextMenuRequested ?

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

                    D 1 Reply Last reply
                    0
                    • SGaistS SGaist

                      Do you mean in the slot connected to customContextMenuRequested ?

                      D Offline
                      D Offline
                      DMIII
                      wrote on last edited by DMIII
                      #10

                      @SGaist Yes, I open the QGraphicsView context menu in the slot connected to customContextMenuRequested. I don't know if the use of underMouse() is convenient or not.

                      JonBJ 1 Reply Last reply
                      0
                      • D DMIII

                        @SGaist Yes, I open the QGraphicsView context menu in the slot connected to customContextMenuRequested. I don't know if the use of underMouse() is convenient or not.

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

                        @DMIII
                        I may be off-base here, but have a look at http://www.qtcentre.org/archive/index.php/t-65353.html:

                        , the graphicsView context menu is beig shown, if I right-click on it, not one of the item.

                        Look at the example code in QGraphicsView::itemAt(). You will need to implement something like that in your showContextMenu() slot - first check to see if there is an item at the QPoint. If there is, and it is one of your custom items, then you need to display its context menu (maybe send it a QGraphicsSceneContextMenuEvent). If there is nothing at the QPoint, then you display the QGraphicsView's context menu. The problem you are having is that the view's context menu handler is eating the right-clicks and your graphics items never see them.

                        I have no idea, but don't you need to implement similar principle for the QWidget you are adding? In other words, it's your QGraphicsView job to recognise the right-click and forward it to the desired object.

                        D 1 Reply Last reply
                        1
                        • JonBJ JonB

                          @DMIII
                          I may be off-base here, but have a look at http://www.qtcentre.org/archive/index.php/t-65353.html:

                          , the graphicsView context menu is beig shown, if I right-click on it, not one of the item.

                          Look at the example code in QGraphicsView::itemAt(). You will need to implement something like that in your showContextMenu() slot - first check to see if there is an item at the QPoint. If there is, and it is one of your custom items, then you need to display its context menu (maybe send it a QGraphicsSceneContextMenuEvent). If there is nothing at the QPoint, then you display the QGraphicsView's context menu. The problem you are having is that the view's context menu handler is eating the right-clicks and your graphics items never see them.

                          I have no idea, but don't you need to implement similar principle for the QWidget you are adding? In other words, it's your QGraphicsView job to recognise the right-click and forward it to the desired object.

                          D Offline
                          D Offline
                          DMIII
                          wrote on last edited by
                          #12

                          @JonB Thank you for the link. I will try to implement this, but what is the difference between this and the solution I mentioned above ? I don't know if it is really important that the QGraphicsView recognise the right click.

                          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