cancel
Showing results for 
Search instead for 
Did you mean: 

Get a pointer of the view in a container (container member of that view)

Professional
Senior

Hello

how can we get a pointer to the current view in a container (to get for example a variable value contained in the view object)

thanks

best regards

16 REPLIES 16

This ofcourse requires the setViewPtr() method to be something else like setViewCallback() instead.

Sorry but i don't understand... have you a piece of software example ?

thanks

I don't have an example of this on me, but in the same way that you set a callback on a touchgfx::Button (with associated handler - The source code for Button/AbstractButton is available), you just set the callback on your custom container (instead of the pointer to the view). Your custom container will then use this callback (to the view) to communicate values instead of a pointer. You can check the Button/Asbstract button code again to see how to execute a callback at will using the execute() method. You have to write this code yourself - The declaration and definition of the callback as well as the handler - There is currently no way to create that kind of custom signal between container and view through the designer).

I don't know what kind of data you want to send to your views through that callback. If i get some time i could maybe try to create an example for you.

Thanks for your reply , i don't understand the interest of a container, if it can't be included in different screens, and then call a generic callback of these views...

I would appreciate an example of Screen1View, Screen2View including a container, and in this container beeing able to call a callback of Screen1View or Screen2View on a press of a box in the container for example (Screen1View callback is called if this screen is active, Screen2View callback is called if this screen is active)

thanks

best regards

You can use a pointer if you want, but then you need to make sure that the base class you're referencing has enough common functionality to satisfy your needs. In either case you'll have to cast your pointer in your custom container to something so you can call specific, concrete view methods which makes it tied to a type. It sounds like you want to have a generic container - If you choose to operate on pointers to views you'll be tying it to particular views or base view classes which defeats the purpose.

By using generic callbacks instead of a pointer you can avoid this tight coupling - This is just what it seems like you really want to me. You want to be able to carry the container with you into other projects maybe.

If i have time i'll try to provide examples of both and show the benefits of one over the other. e.g. if you use callbacks, you'll need to parse the value passed through the callback while when using pointers you can simply just call a method on the view class.

Martin KJELDSEN
Chief III

Here's an example using callbacks that i just made. A project that defines a screen with two custom containers (Both CustomContainer1). CustomContainer1 has a background and a ClickListener Box. When the Box is pressed, the CustomContainer catches this in an internal handler and then calls a Callback (set by the view) to communicate it's identity back. Based on this identity (The first or the second custom container) it will show/hide the custom containers.

I've written some comments in the code - Let me know if you need more information.

Martin KJELDSEN
Chief III

The benefit of using this approach with callbacks is that it's generic and your containers do not need to store a pointer to a specific view type (Even if it's a base view). You could have the handler and callback for the views that use this custom container in a baseview.