Get container size for Custom UI Component
Afficher commentaires plus anciens
Hi
I would like to implement costum UI's in the app designer. For simplicity I'm using the IPAddressComponent example.
https://www.mathworks.com/help/matlab/ref/matlab.ui.componentcontainer.componentcontainer-class.html
I'm able to place the UI component in different uifigures and in specified uipanels from command line.
What I can't figure out is how I get the size of the container I'm placing the component inside. Putting a breakpoint in the setup function does not give me any information except the object itself. If I go back in the function call stack I do see a varargin, with the container handles i'm looking for.
% place in uifigure
f1 = uifigure;
f2 = uifigure;
h1 = IPAddressComponent(f1)
% place in uipanel
fig = uifigure;
p = uipanel(fig,'Position',[20 20 300 135]);
h1 = IPAddressComponent(p)
Can any of you tell me how I can get the size/position of the provided container so that I can create my component according to its size?
Regards Heine
3 commentaires
Mario Malic
le 25 Fév 2021
Modifié(e) : Mario Malic
le 25 Fév 2021
This seem a bit impractical, but I am not sure whether this class is supposed to see anything you pass into it. You probably would have to redefine the setup function, but I wouldn't know how.
p = uifigure;
h1 = IPAddressComponent(p, 'Position', [0 0 p.Position(3) p.Position(4)]);
If you save the handle to the component, you can get Parent properties once the object is created. Execute changes afterwards, I think that's how it works in AppDesigner.
p.Visible = 'off'
h1 = IPAddressComponent(p)
h1.Position = h1.Parent.Position; % consider also units here, some might be relative
% other components
p.Visible = 'on'
Heine Hørup Pedersen
le 25 Fév 2021
Mario Malic
le 25 Fév 2021
Modifié(e) : Mario Malic
le 25 Fév 2021
I have edited the comment, I am not sure whether it's worthy to chase the implementation from handle classes (i have never worked with it).
Another suggestion would be to add a function in each component to set it to size of parent component so you can later invoke it on all handles that need resizing.
Réponse acceptée
Plus de réponses (1)
Greg
le 15 Avr 2021
0 votes
I strongly encourage using uigridlayouts. Everywhere. Every componentcontainer I build starts with a grid, and every app has a grid at the base of the figure. Using grids makes nearly all conversations about size and position irrelevant.
To your specific question though, the setup method executes before any input arguments are processed. To obtain the parent of the componentcontainer, you need to do it in the constructor, or the update method. In either of those two methods, a simple obj.Parent.Position will give you the information you want.
Catégories
En savoir plus sur Creating, Deleting, and Querying Graphics Objects dans Centre d'aide et File Exchange
Produits
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!