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.
  • DiracsbracketD Offline
    DiracsbracketD Offline
    Diracsbracket
    wrote on last edited by Diracsbracket
    #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?

    jsulmJ 1 Reply Last reply
    0
    • DiracsbracketD Diracsbracket

      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?

      jsulmJ Offline
      jsulmJ Offline
      jsulm
      Lifetime Qt Champion
      wrote on last edited by jsulm
      #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

      DiracsbracketD 2 Replies Last reply
      3
      • jsulmJ jsulm

        @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.

        DiracsbracketD Offline
        DiracsbracketD Offline
        Diracsbracket
        wrote on 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?

        jsulmJ 1 Reply Last reply
        0
        • DiracsbracketD Diracsbracket

          @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?

          jsulmJ Offline
          jsulmJ Offline
          jsulm
          Lifetime Qt Champion
          wrote on 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

          DiracsbracketD 1 Reply Last reply
          0
          • jsulmJ jsulm

            @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).

            DiracsbracketD Offline
            DiracsbracketD Offline
            Diracsbracket
            wrote on 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
            • jsulmJ jsulm

              @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.

              DiracsbracketD Offline
              DiracsbracketD Offline
              Diracsbracket
              wrote on 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

              • Login

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