cancel
Showing results for 
Search instead for 
Did you mean: 

Can you call a method on view from a custom container?

Professional
Senior

Hello, can someone help me on the current question already posted :

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

1 ACCEPTED SOLUTION

Accepted Solutions

Yes. I've created an example for you (4.10.0). I took inspiration from your old project having a custom container with a box as a clickable element to make it familiar. When clicking that box, it then calls a method on the view.

Have a look. I removed the touchgfx folder to keep the size down. You can substitute that with your own or simply import this gui into another application.

View solution in original post

18 REPLIES 18
Martin KJELDSEN
Chief III

Hi @Professional​,

I'm back from travels. The easiest thing is to take a look at the Button widget. It does exactly what you're after. It takes a callback that you define a handler for in the view, and when you press the button, internally, the callback is fired and execution will enter your handler. You should have the code for Button inside touchgfx/widgets.

Let me know if you need more pointers!

Best regards,

Martin

Hello again!

i don't understand : the pb is not that the callback is not fired in the container (as a button in a view). The problem is to transmit this callback to the view integrating this container (several view can have this container, and action can be different according to the view...)

regards

Hi Professional,

If you create a simple designer project with a button and an interaction that reacts to that button, you'll see what i mean. Here's the code the designer generates - I've posted parts of the baseview cpp and hpp file. You'll see that :

  1. the buttonCallback is defined in the view base constructor. Here, the callback is tied to a handler method called buttonCallbackHandler() which is defined further down.
  2. The callback is templated with the recipient of the callback as well as a reference to the object responsible for the callback. And the handler takes just the responsible object reference.
  3. Once you set the action on the button, clicking it (due to button internals) should result in the handler method getting called.

I'm not sure if this helps clarify the process.

Screen1ViewBase::Screen1ViewBase() :
    buttonCallback(this, &Screen1ViewBase::buttonCallbackHandler)
{
    box1.setPosition(0, 0, 800, 480);
    box1.setColor(touchgfx::Color::getColorFrom24BitRGB(255, 24, 24));
 
    button1.setXY(246, 154);
    button1.setBitmaps(Bitmap(BITMAP_BLUE_BUTTONS_ROUND_EDGE_SMALL_ID), Bitmap(BITMAP_BLUE_BUTTONS_ROUND_EDGE_SMALL_PRESSED_ID));
    button1.setAction(buttonCallback);
 
    add(box1);
    add(button1);
}
 
void Screen1ViewBase::setupScreen()
{
 
}
 
void Screen1ViewBase::buttonCallbackHandler(const touchgfx::AbstractButton& src)
{
    if (&src == &button1)
    {
        //Do something based on button 1 clicked
   
    }
}
...
 
private:
 
    /*
     * Callback Handler Declarations
     */
    void buttonCallbackHandler(const touchgfx::AbstractButton& src);
 
    /*
     * Callback Declarations
     */
    touchgfx::Callback<Screen1ViewBase, const touchgfx::AbstractButton&> buttonCallback;
 
};
 
#endif // SCREEN1_VIEW_BASE_HPP

Hello

thanks for your reply, but this partly what i want to do : in my case, in the callback of a container i want to call another callback of the view containing this container. Going to the callback of the container is Ok, but then i would like to call a callback of the view

1) in the view pass a pointer of the view to the container

cont_home_warning1.setViewPtr(this);

2)in the container

void setViewPtr(touchgfx::View<Presenter> *v);

void Cont_home_warning::setViewPtr(touchgfx::View<Presenter> *v)

{

view_prt = v;

}

3) then call in the container callback th callback of the view

if (v != null)

{v->callback();}

but this doesn't compile..

How I can you pass a pointer to a container ?

Thanks

Hello,

can some one help me on this subject

thanks

regards

Hi,

Can you please provide a build log? Maybe even your project.

Best regards,

Martin

hello

in this project there is a container, a click on its box1 goes to a callback, but from that callback i want to call a function (callback) of the view that have the container.

How can i do this (pass the pointer this of the view to the container) ?

thanks

Hi,

The project is not immediately working for me (some files missing) and i'm not seeing a lot of code. Only a custom container with a Box (ClickListener). Do i have the right files? You said you'd done some work on the actual container trying to store a pointer to the view. I also noticed it's a 4.8.0 project - You should consider upgrading to 4.10.0 which has a lot of new features.

Thanks!

Hello

don't know why the zip is not working (i use 4.10)

just a piece of code :

how can i jump from time_boxClickHandler to a callback in the view ?

thanks

CustomContainer1::CustomContainer1() :

   BoxClickedCallback(this, &CustomContainer1::time_boxClickHandler)

{

   box1.setClickAction(BoxClickedCallback);

}

void CustomContainer1::time_boxClickHandler(const Box& b, const ClickEvent& evt)

{

   //treat only pressed event

   if (evt.getType() != ClickEvent::PRESSED) return;

   if (&b == &box1)

   {

   }

}