Skip to main content
Professional
Associate III
Jan 14, 2019 1:56 PM UTC
Solved

Callback from a common custom container to the current view

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.
Martin KJELDSEN
Martin KJELDSENBest answer
Principal III
Feb 5, 2019 9:52 AM UTC

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.

19 replies

Martin KJELDSEN
Principal III
Jan 15, 2019 8:35 AM UTC

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
Jan 15, 2019 8:42 AM UTC

Thanks a lot for your support !!!

Professional
Associate III
Jan 16, 2019 7:36 AM UTC

Hello,

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

thanks

Martin KJELDSEN
Martin KJELDSENBest answer
Principal III
Feb 5, 2019 9:52 AM UTC

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 3:20 PM UTC

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 4:25 PM UTC

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
Feb 5, 2019 10:13 AM UTC

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
Feb 5, 2019 10:50 AM UTC

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
Feb 5, 2019 11:56 AM UTC

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

VKyri
Associate
Jul 18, 2019 4:32 PM UTC

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
Aug 5, 2019 12:50 PM UTC

Hi @VKyri​,

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

/Martin

Professional
Associate III
Aug 5, 2019 12:59 PM UTC

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
Aug 5, 2019 1:05 PM UTC

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