addChild for uifigure components

隐身守侯 提交于 2019-12-11 11:29:08

问题


I'm trying to develop my first GUIs with uifigure (programmatically, not with appdesigner [but I have added it as a keyword since it is related]), and (as expected) I'm missing some of the extended features and widgets as provided by the GUI Layout Toolbox and the Widgets Toolbox for standard java figures.

Thus, I tried to change some widgets that I had developed to uifigure, and uigridlayout seems to be quite handy to replace the uix.VBox and uix.HBox from the GUI Layout Toolbox.

For standard java figures, assume I have a class MyWidget and a corresponding instance mywidget of it. MyWidget would be, finally, an ancestor of matlab.ui.container.internal.UIContainer which provides an addChild method which can be overriden to customize the behaviour of

  uicontrol(mywidget)

I'm looking for the same for uifigure components. Assume the follwing class derived from matlab.ui.container.GridLayout, which is the class of the result of a uigridlayout call.

classdef MyGrid < matlab.ui.container.GridLayout
    methods
        function self = MyGrid(varargin)
            self = self@matlab.ui.container.GridLayout(varargin{:});
        end
    end
    methods ( Access = protected )

        function addChild(self, child)
            disp('hooray');
            addChild@matlab.ui.container.GridLayout(self, child);
        end
    end
end

When I now initiate a MyGrid instance

g = MyGrid()

everything looks good:

g = 

  MyGrid with properties:

    RowHeight: {'1x'  '1x'}
    ColumnWidth: {'1x'  '1x'}

but adding a child to it does not call the addChild method:

>> uibutton(g)

ans = 

  Button (Button) with properties:

               Text: 'Button'
               Icon: ''
    ButtonPushedFcn: ''
           Position: [100 100 100 22]

Note: No output of hooray above. The Parent property is correct:

>> ans.Parent

ans = 

  MyGrid with properties:

      RowHeight: {'1x'  '1x'}
    ColumnWidth: {'1x'  '1x'}

  Show all properties

From this I guess that addChild is not the method used (at least by matlab.ui.container.GridLayout) to add a child.

Does anyone know the mechanism of adding a child to a container in an uifigure component?


回答1:


I don't know why I didn't look there yesterday, but the code of matlab.ui.container.GridLayout has the (protected) method

function handleChildAdded(obj, childAdded)
    obj.processChildAdded(childAdded);

    obj.addChildLayoutPropChangedListener(childAdded);

    obj.updateImplicitGridSize('childAdded', childAdded);

    obj.updateLastCell('childAdded', childAdded);
end

The method processChildAdded might be better for my purposes, but is private. Anyway, handleChildAdded works:

classdef MyGrid < matlab.ui.container.GridLayout
    methods
        function self = MyGrid(varargin)
            self = self@matlab.ui.container.GridLayout(varargin{:});
        end
    end
    methods ( Access = protected )
        function handleChildAdded(self, child)
            disp('hooray');
            handleChildAdded@matlab.ui.container.GridLayout(self, child);
        end
    end
end
>> g=MyGrid();
>> uibutton(g);
hooray


来源:https://stackoverflow.com/questions/58758438/addchild-for-uifigure-components

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!