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. GraphicsView::contextMenuEvent(QContextMenuEvent*) conficts with LineEdit::contextMenuEvent

GraphicsView::contextMenuEvent(QContextMenuEvent*) conficts with LineEdit::contextMenuEvent

Scheduled Pinned Locked Moved Solved General and Desktop
5 Posts 3 Posters 482 Views 1 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.
  • Petross404_Petros SP Offline
    Petross404_Petros SP Offline
    Petross404_Petros S
    wrote on last edited by
    #1

    Hi all. I have a custom Graphics{View, Scene} and an embedded LineEdit to input some numbers.

    Because I need to right-click on my LineEdit and be able to copy, paste etc, I used LineEdit::contextMenuEvent:

    void LineEdit::contextMenuEvent( QContextMenuEvent* event )
    {
    	QMenu* menu = createStandardContextMenu();
    	menu->setStyleSheet( normalStyleSheet );
    	menu->exec( event->globalPos() );
    	delete menu;
    }
    

    I don't forward the event to QLineEdit though and it works beautifully.

    At the same time, I want to right-click on the GraphicsView and select some actions related to my view.

    void GraphicsView::contextMenuEvent( QContextMenuEvent* event )
    {
            // ....
    	menu.exec( event->globalPos() );
    
    	QGraphicsView::contextMenuEvent(event);
    }
    

    Now, the problem occurs when I right click on LineEdit object. I get LineEdit's menu and after is closed GraphicsView's too.

    How can I let my GraphicsView know that when the right click is on top of another widget (let's say LineEdit), it will ignore the event for itself. Maybe an event filter is a better alternative?

    Thank you in advance.

    JonBJ jeremy_kJ 2 Replies Last reply
    0
    • Petross404_Petros SP Petross404_Petros S

      Hi all. I have a custom Graphics{View, Scene} and an embedded LineEdit to input some numbers.

      Because I need to right-click on my LineEdit and be able to copy, paste etc, I used LineEdit::contextMenuEvent:

      void LineEdit::contextMenuEvent( QContextMenuEvent* event )
      {
      	QMenu* menu = createStandardContextMenu();
      	menu->setStyleSheet( normalStyleSheet );
      	menu->exec( event->globalPos() );
      	delete menu;
      }
      

      I don't forward the event to QLineEdit though and it works beautifully.

      At the same time, I want to right-click on the GraphicsView and select some actions related to my view.

      void GraphicsView::contextMenuEvent( QContextMenuEvent* event )
      {
              // ....
      	menu.exec( event->globalPos() );
      
      	QGraphicsView::contextMenuEvent(event);
      }
      

      Now, the problem occurs when I right click on LineEdit object. I get LineEdit's menu and after is closed GraphicsView's too.

      How can I let my GraphicsView know that when the right click is on top of another widget (let's say LineEdit), it will ignore the event for itself. Maybe an event filter is a better alternative?

      Thank you in advance.

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

      @Petross404_Petros-S said in GraphicsView::contextMenuEvent(QContextMenuEvent*) conficts with LineEdit::contextMenuEvent:

      Now, the problem occurs when I right click on LineEdit object. I get LineEdit's menu and after is closed GraphicsView's too.

      Not sure but: did you try putting event->accept() at the end of void LineEdit::contextMenuEvent( QContextMenuEvent* event ), to indicate your handler has dealt with the event and does not want it passed on?

      1 Reply Last reply
      0
      • Petross404_Petros SP Offline
        Petross404_Petros SP Offline
        Petross404_Petros S
        wrote on last edited by
        #3

        Thanks for the answer. I did just now and I am afraid it doesn't help.

        1 Reply Last reply
        0
        • Petross404_Petros SP Petross404_Petros S

          Hi all. I have a custom Graphics{View, Scene} and an embedded LineEdit to input some numbers.

          Because I need to right-click on my LineEdit and be able to copy, paste etc, I used LineEdit::contextMenuEvent:

          void LineEdit::contextMenuEvent( QContextMenuEvent* event )
          {
          	QMenu* menu = createStandardContextMenu();
          	menu->setStyleSheet( normalStyleSheet );
          	menu->exec( event->globalPos() );
          	delete menu;
          }
          

          I don't forward the event to QLineEdit though and it works beautifully.

          At the same time, I want to right-click on the GraphicsView and select some actions related to my view.

          void GraphicsView::contextMenuEvent( QContextMenuEvent* event )
          {
                  // ....
          	menu.exec( event->globalPos() );
          
          	QGraphicsView::contextMenuEvent(event);
          }
          

          Now, the problem occurs when I right click on LineEdit object. I get LineEdit's menu and after is closed GraphicsView's too.

          How can I let my GraphicsView know that when the right click is on top of another widget (let's say LineEdit), it will ignore the event for itself. Maybe an event filter is a better alternative?

          Thank you in advance.

          jeremy_kJ Offline
          jeremy_kJ Offline
          jeremy_k
          wrote on last edited by
          #4

          @Petross404_Petros-S said in GraphicsView::contextMenuEvent(QContextMenuEvent*) conficts with LineEdit::contextMenuEvent:

          void GraphicsView::contextMenuEvent( QContextMenuEvent* event )
          {
                  // ....
          	menu.exec( event->globalPos() );
          
          	QGraphicsView::contextMenuEvent(event);
          }
          

          QGraphicsView::contextMenuEvent() will attempt to call QGraphicsScene::contextMenuEvent() if the view is associated with a scene:

          The default implementation forwards the event to the topmost visible item that accepts context menu events at the position of the event. If no items accept context menu events at this position, the event is ignored.

          This looks like the source of the problem, although the order of the menus in the description of the symptom is opposite of what the source shows. GraphicsView::contextMenuEvent() should call the base version, and only show its own menu if the event was ignored.

          LineEdit::contextMenuEvent() should mark the event as accepted to facilitate this logic.

          Asking a question about code? http://eel.is/iso-c++/testcase/

          1 Reply Last reply
          0
          • Petross404_Petros SP Offline
            Petross404_Petros SP Offline
            Petross404_Petros S
            wrote on last edited by
            #5

            I removed the GraphicsView handler after all. It wasn't worth it.

            Thank you very much for your help.

            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