Multiple objects sharing a context menu?
显示 更早的评论
Hi all,
I'm trying to have multiple objects in the same figure share a common context menu. In the menu callback, is there a way to tell which one of the objects initiated the callback (i.e. was right-clicked on)? Or do I have to create a separate contextmenu object for each one of them?
Thanks!
Niko
6 个评论
Adam
2015-8-3
Does the source (1st argument) of the callback not tell you? I can't remember off-hand what the source returns for a context menu, but usually it is the object that triggered the callback.
Niko
2015-8-3
Niko
2015-8-21
Adam
2015-8-21
I would probably do it by creating a class to wrap up the context menu, add any extra information required to its callback and just create an instance of that class for each place where it is needed, but if you aren't familiar with Matlab OOP that would be quite a steep learning curve possibly for this example.
Niko
2015-8-21
Adam
2015-8-21
You can set the callback and when you do you can pass in other arguments than just those that come automatically. If your context menu is embedded in a class you only need to define the context menu once, but the class can also contain e.g. a line object handle or whatever else it needs to allow it to link together the context menu and the thing it is being attached to.
Then you would create and instance of your class for each context menu you want, each instance of the class would link to a different graphics object which it would then pass into the context menu's callback to allow you to get the context specific information.
采纳的回答
更多回答(3 个)
Phil
2016-10-4
4 个投票
There is a simpler answer. You can use "gco" in callbacks called by the context menu to get the handle of the current graphics object that was clicked. Thus the menu can be shared between objects without difficulty and still have different behavior for each object. For example, to delete the specific object clicked, use the callback "@(~,~)delete(gco)".
4 个评论
Kristoffer Walker
2018-11-5
This is a much better answer. Thanks Phil! What I did was this in my callback:
ax = gco;
Kris
Kristoffer Walker
2020-10-21
Folks,
This does not work with AppDesigner. Is there a similar simple solution for AppDesigner? This is a fundamental need for developers. Many thanks to all of you who contribute to knowledge sharing on this forum!!
Kris
@Kristoffer Walker, that's because the HandleVisibility of uifigures is set to 'off' by default which prevents getting access to the figure handle or any object within the figure.
solution 1: set the uifigure's HandleVisibility to 'callback' or 'on'--(not recommended)
solution 3 (best): instead of relying on the object being 'current', pass the object's handles into the function or use a pre-defined obj handle in the function definition.
래충 강
2021-12-17
2 个投票
get(app.UIFigure, 'CurrentObject')
It tell you which object called the context menu
1 个评论
Rina Blomberg
2023-2-3
编辑:Rina Blomberg
2023-2-3
Thank you!! Exactly the solution I needed.
Walter Roberson
2015-8-3
0 个投票
You could Label or Tag them differently; http://www.mathworks.com/help/matlab/ref/uicontextmenu.html
3 个评论
Niko
2015-8-3
Walter Roberson
2015-8-3
No, notice that the single UiContextMenu callback is used in the example there and that it pulls information out of the Label (in that case) to decide what to do. It is not well-documented in the description of uicontextmenu what the source object is (the first parameter passed in) but you can deduce from the example that it is the menu object rather than the uicontextmenu object
类别
在 帮助中心 和 File Exchange 中查找有关 Interactive Control and Callbacks 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!