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. Different Context Menu for QGraphicsObject in QGraphicsScene
Forum Updated to NodeBB v4.3 + New Features

Different Context Menu for QGraphicsObject in QGraphicsScene

Scheduled Pinned Locked Moved Solved General and Desktop
4 Posts 2 Posters 2.0k 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.
  • ModelTechM Offline
    ModelTechM Offline
    ModelTech
    wrote on last edited by ModelTech
    #1

    In creating a graphical editor, I have reimplemented QGraphicsScene. Part of this includes the definition of a context menu by reimplementing QGraphicsScene::contextMenuEvent(QGraphicsSceneContextMenuEvent *). The code looks like this:

    void MyScene::contextMenuEvent(QGraphicsSceneContextMenuEvent *Event) {
        // Create QActions
        // Connect signals
        QMenu *Menu = new QMenu;
        // Add actions to menu
        Menu->popup(Event->screenPos());
        Event->accept();
    }
    

    I also created a subclass of QGraphicsObject for drawing my graphics item. For this graphics item, I also want to have a context menu, but a different one. And I defined it by reimplementing QGraphicsObject::contextMenuEvent(QGraphicsSceneContextMenuEvent *). That code looks as follows

    void MyItem::contextMenuEvent(QGraphicsSceneContextMenuEvent *Event) {
        // Create QActions
        // Connect Signals
        QMenu *Menu = new QMenu;
        // Add actions to menu
        Menu->popup(Event->screenPos());
        Event->accept();
    }
    

    Unfortunately, the context menu of MyItem does not show up when I right-click on the item in the scene. Instead it still shows the context menu of the scene. Any idea on how I can get the context menu of MyItem when I right-click on the item while having the scene context menu when right-clicking anywhere else?

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

      Hi,

      There was a mail sent to the interest mailing list mentioning this.

      You should check the provided workaround.

      Hope it helps

      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
      1
      • ModelTechM Offline
        ModelTechM Offline
        ModelTech
        wrote on last edited by ModelTech
        #3

        Great, this helped me to make it work. However, I do have another question now as I originally reimplemented QGraphicsScene instead of QGraphicsView. This solution assumes reimplementation of QGraphicsView. What is generally the best approach / the difference between the two approaches?

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

          The scene can be shared between several views which might not all be concerned by that menu.

          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

          • Login

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