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. Adding existing QMenu from parent to context menu of child
Forum Updated to NodeBB v4.3 + New Features

Adding existing QMenu from parent to context menu of child

Scheduled Pinned Locked Moved Solved General and Desktop
6 Posts 2 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
    Diracsbracket
    wrote on 6 Nov 2017, 08:56 last edited by Diracsbracket 11 Jun 2017, 08:57
    #1

    Hi,
    I have a MainWindow app with some sub menu that controls some plot settings.

    The plot widget inside a dock, and when right-clicked shows a custom context menu. I would like to reuse the plot settings menu or some individual actions from it in the context menu of the plot.

    I have tried by creating a public method in the parent to return the pointer to the QMenu in question, and then tried to add that menu to my context menu:

    QMenu* rangeMenu = ((MainWindow*)parent())->getRangeMenu();
    
    if (rangeMenu)
         menu.addMenu(rangeMenu);
    
    menu.exec(event->globalPos());
    

    But this crashes.

    Is there a way to achieve what I want?

    J 1 Reply Last reply 6 Nov 2017, 12:34
    0
    • D Diracsbracket
      6 Nov 2017, 08:56

      Hi,
      I have a MainWindow app with some sub menu that controls some plot settings.

      The plot widget inside a dock, and when right-clicked shows a custom context menu. I would like to reuse the plot settings menu or some individual actions from it in the context menu of the plot.

      I have tried by creating a public method in the parent to return the pointer to the QMenu in question, and then tried to add that menu to my context menu:

      QMenu* rangeMenu = ((MainWindow*)parent())->getRangeMenu();
      
      if (rangeMenu)
           menu.addMenu(rangeMenu);
      
      menu.exec(event->globalPos());
      

      But this crashes.

      Is there a way to achieve what I want?

      J Offline
      J Offline
      jsulm
      Lifetime Qt Champion
      wrote on 6 Nov 2017, 12:34 last edited by jsulm 11 Jun 2017, 12:36
      #2

      @Diracsbracket Are you sure parent of the widget is MainWindow? Don't use C style casts in C++.
      You should check the pointer before dereferencing it:

      MainWindow* mainWindow = qobject_cast<MainWindow*>(parent());
      if (mainWindow) {
          QMenu* rangeMenu = mainWindow->getRangeMenu();
          if (rangeMenu)
              menu.addMenu(rangeMenu);
      }
      menu.exec(event->globalPos());
      

      Actually child widgets should not know any implementation details of their parent. Better is to pass needed data from parent to child.

      https://forum.qt.io/topic/113070/qt-code-of-conduct

      D 2 Replies Last reply 7 Nov 2017, 08:41
      3
      • J jsulm
        6 Nov 2017, 12:34

        @Diracsbracket Are you sure parent of the widget is MainWindow? Don't use C style casts in C++.
        You should check the pointer before dereferencing it:

        MainWindow* mainWindow = qobject_cast<MainWindow*>(parent());
        if (mainWindow) {
            QMenu* rangeMenu = mainWindow->getRangeMenu();
            if (rangeMenu)
                menu.addMenu(rangeMenu);
        }
        menu.exec(event->globalPos());
        

        Actually child widgets should not know any implementation details of their parent. Better is to pass needed data from parent to child.

        D Offline
        D Offline
        Diracsbracket
        wrote on 7 Nov 2017, 08:41 last edited by
        #3

        @jsulm said in Adding existing QMenu from parent to context menu of child:

        Don't use C style casts in C++.
        You should check the pointer before dereferencing i

        You're right, I should check the pointer.

        Actually child widgets should not know any implementation details of their parent. Better is to pass needed data from parent to child.

        So you mean that I should create the items for the plot menu in the plot child itself and then access them from the main app for use in the main app's menu? I was also wondering about the best way to do this, and I agree this is probably the way to go.

        But can you confirm this will work as expected, i.e. if the Main app uses an action from the child in one of the Main menu items, and this item is triggered in the Main app (not in the child's context menu), the corresponding action in the child will be triggered?

        J 1 Reply Last reply 7 Nov 2017, 08:47
        0
        • D Diracsbracket
          7 Nov 2017, 08:41

          @jsulm said in Adding existing QMenu from parent to context menu of child:

          Don't use C style casts in C++.
          You should check the pointer before dereferencing i

          You're right, I should check the pointer.

          Actually child widgets should not know any implementation details of their parent. Better is to pass needed data from parent to child.

          So you mean that I should create the items for the plot menu in the plot child itself and then access them from the main app for use in the main app's menu? I was also wondering about the best way to do this, and I agree this is probably the way to go.

          But can you confirm this will work as expected, i.e. if the Main app uses an action from the child in one of the Main menu items, and this item is triggered in the Main app (not in the child's context menu), the corresponding action in the child will be triggered?

          J Offline
          J Offline
          jsulm
          Lifetime Qt Champion
          wrote on 7 Nov 2017, 08:47 last edited by
          #4

          @Diracsbracket Actually my idea was that main window passes the menu to the child when it creates the child (or at some other point - the child could have a setter method for that).

          https://forum.qt.io/topic/113070/qt-code-of-conduct

          D 1 Reply Last reply 7 Nov 2017, 09:02
          0
          • J jsulm
            7 Nov 2017, 08:47

            @Diracsbracket Actually my idea was that main window passes the menu to the child when it creates the child (or at some other point - the child could have a setter method for that).

            D Offline
            D Offline
            Diracsbracket
            wrote on 7 Nov 2017, 09:02 last edited by
            #5

            @jsulm
            Thanks. I have tried it and it works!

            However, my position is that since the menu is related to the plot itself, it should be created by the child, and passed to the parent, if the parent asks for it via a getter method. That's what I tried just now, and it seems to be cleaner/logical in my case.

            1 Reply Last reply
            0
            • J jsulm
              6 Nov 2017, 12:34

              @Diracsbracket Are you sure parent of the widget is MainWindow? Don't use C style casts in C++.
              You should check the pointer before dereferencing it:

              MainWindow* mainWindow = qobject_cast<MainWindow*>(parent());
              if (mainWindow) {
                  QMenu* rangeMenu = mainWindow->getRangeMenu();
                  if (rangeMenu)
                      menu.addMenu(rangeMenu);
              }
              menu.exec(event->globalPos());
              

              Actually child widgets should not know any implementation details of their parent. Better is to pass needed data from parent to child.

              D Offline
              D Offline
              Diracsbracket
              wrote on 7 Nov 2017, 09:04 last edited by
              #6

              @jsulm said in Adding existing QMenu from parent to context menu of child:

              Are you sure parent of the widget is MainWindow

              You're right. MainWindow was not the parent.

              1 Reply Last reply
              0

              3/6

              7 Nov 2017, 08:41

              • Login

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