Skip to main content
Professional
Associate III
Jan 22, 2019 7:25 AM UTC
Question

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

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

This topic has been closed for replies.

16 replies

Martin KJELDSEN
Principal III
Jan 24, 2019 1:46 PM UTC

Hi @Professional​,

You could pass "this" from your view to the container.

Best regards,

Martin

Professional
Associate III
Jan 24, 2019 1:55 PM UTC

Hello,

thank for your answer but concretly i don't know how to do this : where is called the constructor of the container ?

i have the class :

class TimeView : public TimeViewBase

in the base class i have the container   Cont_menu_bar cont_menu_bar1; declared.

But the creation of that is done in touchGFX generated code.

cont_menu_bar1 can fire a callback placed in the container, but i would like to call a TimeView callback (getting this may make it work..)

regards

Martin KJELDSEN
Principal III
Jan 24, 2019 1:58 PM UTC

If it's a custom container you should have a concrete implementation of this container. You can add a method there to set the pointer for the container and call that during setupScreen() of your view:

e.g.

::setupScreen()

{

myContainer.setViewPtr(this);

}

Professional
Associate III
Jan 24, 2019 2:24 PM UTC

yes i have tried to have a function in my container.hpp and cpp :

#include <mvp/View.hpp>

void setViewPointer(View * this);

but i can't compile that as View is not defined...

have you a complete example ?

thanks

regards

Martin KJELDSEN
Principal III
Jan 28, 2019 10:32 PM UTC

Just to clarify. I've posted an example of how to do this in your other post. Basically you want a pointer to your concrete view, e.g. MyView in order to access whatever methods you want. And in order to avoid circular references, you have to make a forward declaration to your view from your container.

I'll attach the project here as well.

Professional
Associate III
Jan 29, 2019 7:32 AM UTC

Hello

thank you for your example, but it doesn't solve completly my problem :

you have declared the function void setPtr(Screen1View* ptr);

But i would like to have a generic declaration for the view pointer : function void setPtr(View* ptr);

The goal is to call any kind of view from that container (Screen1View, Screen2View...)

How can i do this ? (i have trouble to compile such a declaration)

Thanks for your support

Martin KJELDSEN
Principal III
Feb 5, 2019 9:50 AM UTC

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
Principal III
Feb 5, 2019 12:09 PM UTC

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.