MATLAB Answers

2

How can I control the stacking order of objects in appdesigner?

Kevin J. Delaney さんによって質問されました 2018 年 1 月 4 日
最新アクティビティ Wouter
さんによって 回答されました 2018 年 2 月 14 日
How can I control the stacking order of objects in appdesigner? It seems to be determined by the order in which the panels or axes are created. What if I want an object created later to slide underneath earlier objects?

  0 件のコメント

サインイン to comment.

3 件の回答

Sean de Wolski
Answer by Sean de Wolski
on 4 Jan 2018
 Accepted Answer

You can control the order of the 'Children' of a graphics object directly:
fig = uifigure;
panel1 = uipanel(fig, 'BackgroundColor', 'b');
panel2 = uipanel(fig, 'BackgroundColor', 'r');
fig.Children = flip(fig.Children)

  5 件のコメント

Putting this as the first couple lines of the startup function works fine for me:
uipanel(app.UIFigure)
app.UIFigure.Children = flip(app.UIFigure.Children);
Why are you creating a separate uifigure? Just use the one that app designer creates unless you want a second window?
Thanks for the replies. I wasn't really clear about what I was posting there, but I was just trying to show that app.main_figure=uifigure and app.Panel=uipanel(app.main_figure) are the creations of those components that are already happening down in createComponents, that text is completely uneditable. Only the app.main_figure.Children= flip(app.main_figure.Children) portion was being put into startupFcn
To that end, I'm confused as to why you you are suggesting calling the uipanel function again within startupFcn. I already have a panel created, but anyway, if I try what you suggest, it gives me an error of "reference to a cleared variable app"
Anyway, I'm not really trying to programmatically rearrange the components, I just wanted to lay it out correctly once, having already created components. I found a workaround for this, which is to graphically load up components into new panels, and then unload them in the order you want them to be stacked (from back to front).
I'm pretty new to appdesigner, but I'll say it's maddening trying to figure out the simplest things, compared to normal matlab scripting.
I was just copying what you had to show that it works, they were adjacent in the question so assumed it was all in your startup. I wouldn't recommend calling uipanel in startup!
It's definitely a fair enhancement request to have them be adjustable in the component browser on the right.

サインイン to comment.


Answer by Kevin J. Delaney on 7 Feb 2018

Inspired by Sean's answer, I wrote the attached utility for use in the startupFcn:
move_to_bottom(app.UIFigure, app.Axes);

  0 件のコメント

サインイン to comment.


Wouter
Answer by Wouter
on 14 Feb 2018

You could also keep a list of handles; e.g. in the variable background and than move all of these to the background.
hfig = uifigure; % create figure
hax = uiaxes(hfig); % create axis
foreground = plot(hax,rand(1,100),'r'); % create red line
hold(hax,'on');
for ind = 1:10
% this moves the foreground plot to the background...
background(ind) = plot(hax,rand(1,100),'k'); % create 10 ack lines (on top of red line)
end
% move the background lines to the background!
ch = get(hax,'children'); % get all plots from hax
[~,neworder] = sort(ismember(ch,background)); % reorder the handles to move the background lines to the background
set(hax,'children',ch(neworder)); % set the new order of all lines

  0 件のコメント

サインイン to comment.



Translated by