Adding existing QMenu from parent to context menu of child
-
Hi,
I have aMainWindow
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?
-
Hi,
I have aMainWindow
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?
@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.
-
@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.
@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 iYou'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?
-
@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 iYou'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?
@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).
-
@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).
@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.
-
@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.
@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.