Skip to main content
Professional
Associate III
January 14, 2019
Solved

Callback from a common custom container to the current view

  • January 14, 2019
  • 15 replies
  • 4805 views

Hello

I have a custom container common to several screen (with OK button in it).

How can i call a callback of the current screen when a button is pressed in the container ?

I do call the callback of the custom containe, but don't know how to propagate it to the current view.

(for example when OK button is pressed i want to execute the action in each view containing this custom container)

I there an example of this ?

thanks

regards

This topic has been closed for replies.
Best answer by Martin KJELDSEN

Here it is. 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.

15 replies

Martin KJELDSEN
Principal III
January 15, 2019

Hi @Professional​,

Just to let you know i've seen your question. I'm traveling right now, but i I will create an example that shows you how to do this.

Best regards,

Martin

Professional
Associate III
January 15, 2019

Thanks a lot for your support !!!

Professional
Associate III
January 16, 2019

Hello,

do you know when you will be able to create this example ?

thanks

Martin KJELDSEN
Martin KJELDSENBest answer
Principal III
February 5, 2019

Here it is. 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.

Junde
Senior III
May 11, 2024

Hello @Martin KJELDSEN 

Thanks for your demo on this topic, it helps me a lot.

One more question: each time I click the box, it will call the callback twice(press and release the box).

Is there some way that I can listen to the release event only?

Thanks again!

Have a good day!

Junde
Senior III
May 11, 2024

Finally, I got it.

Just checking the event type is enough:

void CustomContainer1::boxClickedHandler(const Box &box,
 const ClickEvent &event) {
 if (event.getType() != ClickEvent::RELEASED) {
 return;
 }

 if (&box == &box2) {
 touchgfx_printf("CustomCotainer: box clicked.\n");

 if (viewCallback && viewCallback->isValid()) {
 viewCallback->execute(type);
 }
 }
}

 

Professional
Associate III
February 5, 2019

Hello thank you for your example;it doesn't compile in visual studio but I will try to make it work in my project !

best regards

Martin KJELDSEN
Principal III
February 5, 2019

I used Visual Studio 2017, so it does work, but you may need to retarget to whatever version you're using.

Martin KJELDSEN
Principal III
February 5, 2019

You can also simply "Run Simulator" from within the designer to compile using gcc. Both should work.

VKyri
Associate
July 18, 2019

Hey. I'm trying, as an example, to use CallBack with the CustomContainer. But I do not use Box, I use FlexButton with include TextArea.

From the Widget Box, the declaration looks like:

touchgfx::ClickListener< touchgfx::Box > box2;

So that the function call looks like:

box2.setClickAction(boxClickedCallback);

When used with FlexButton, the declaration looks like:

touchgfx::ClickListener< touchgfx::TextButtonStyle< touchgfx::BoxWithBorderButtonStyle< touchgfx::ClickButtonTrigger > > > btn0;

And now when:

btn0.setClickAction(boxClickedCallback);

I am getting an error when compiling:

no matching function for call to 'touchgfx :: ClickListener <touchgfx :: TextButtonStyle <touchgfx :: BoxWithBorderButtonStyle <touchgfx :: ClickButtonTrigger>>> :: setClickAction (touchgfx :: Callback <NumPad, const touchgfx :: ClickButtonTrigger &, const touchgfx :: ClickEvent &> &) '

Martin KJELDSEN
Principal III
August 5, 2019

Hi @VKyri​,

Back from vacation here. Did you remember to update your handler to take a box with border?

/Martin

Professional
Associate III
August 5, 2019

Hello,

thank you for your reply. I think we can close this topic, as i found a solution for it...

Best regards

Professional
Associate III
August 5, 2019

My new problem is to draw a circle on each touch screen press (at the position of the press) and on each screen drawn in touchGFX project... if you have any simple idea...

I tried to add an invisible box on all the screen to detect the press, but now the button behind this box are not activated by touchGFX...

thanks best regards