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!