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
Martin KJELDSEN
Chief III

Hi @Professional​,

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

Best regards,

Martin

Professional
Senior

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
Chief III

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
Senior

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
Chief III

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.

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

Then you need to either cast your view pointer (making the container type aware) or pass it a pointer to a base-view which would make it less type aware but still tied to a certain base view with certain capabilities.

I think what you need to do is something else. You need to pass in a new callback which is completely generic that you can define in whatever view you want and then pass whatever values to that view - Based on the values you make the view perform a certain action (You could have this callback functionality in a base view). Does that make sense?

Best regards,

Martin

Hello

thank you for your reply.

The problem is how to pass a generic function pointer to a container and then call this function from the container...

Bests regards

From the handler inside your Custom Container you simply call viewCallback->execute(SOME_VALUE). And because the view hooked the callback to a handler of it's own (regardless of view type) you will hit that handler and can now react to whatever value was passed by the container.