0

I created a basic project on the TouchGFX Designer and wrote a function in my own cpp file using touchgfx library. I want that when the button is clicked ,the function is called to ScreenView.cpp or ScreenViewBase.cpp from my own cpp file and change the color of the box.

This is my cpp file.

#include <touchgfx/Color.hpp>
#include <touchgfx/widgets/Box.hpp>
#include <gui/screen1_screen/Screen1View.hpp>
#include <gui/screen1_screen/Screen1Presenter.hpp>
#include <touchgfx/widgets/Box.hpp>

ChangeColor::ChangeColor()
{
    Screen1View::box1;
    box1.setColor(touchgfx::Color::getColorFrom24BitRGB(51, 168, 35));
    box1.invalidate();
}

This is the Screen1View.cpp where I want to call my function.

#include<gui/ChangeColor.hpp>
#include <touchgfx/Color.hpp>

Screen1View::Screen1View():
    buttonCallback(this, &Screen1View::buttonCallbackHandler)
{

}

void Screen1View::setupScreen()
{
    Screen1ViewBase::setupScreen();
    button1.setAction(buttonCallback);

}

void Screen1View::tearDownScreen()
{
    Screen1ViewBase::tearDownScreen();
}
void Screen1View::buttonCallbackHandler(const touchgfx::AbstractButton& src)
{
    if (&src == &button1)
    {
        //Interaction1
        //When button1 clicked execute C++ code
        //Execute C++ code
        //ChangeColor();
        ChangeColor();
    
    }
}

and this is the Screen1BaseView.hpp where the box is declared

#define SCREEN1VIEWBASE_HPP

#include <gui/common/FrontendApplication.hpp>
#include <mvp/View.hpp>
#include <gui/screen1_screen/Screen1Presenter.hpp>
#include <touchgfx/widgets/Box.hpp>
#include <touchgfx/widgets/Button.hpp>

class Screen1ViewBase : public touchgfx::View<Screen1Presenter>
{
public:
    Screen1ViewBase();
    virtual ~Screen1ViewBase() {}
    virtual void setupScreen();

protected:
    FrontendApplication& application() {
        return *static_cast<FrontendApplication*>(touchgfx::Application::getInstance());
    }

    /*
     * Member Declarations
     */
    touchgfx::Box __background;
    touchgfx::Box box1;
    touchgfx::Button button1;

private:

};

I can't access the box. Is there way to do it?

ezgi
  • 11
  • 3

1 Answers1

0

TouchGFX uses MPV architecture, i.e. Model-Presenter-View. The point is Model and View can not access each other.

In your case, your cpp file play role as a model, so it's not allowed to access element in View. That's the reason why you can't access. You need a Presenter to handle the connection.

Sean
  • 79
  • 5
  • A new presenter means a new view, am I wrong? So I'm gonna try to access the presenter that already exists. How can I do it? Do you have an example code? – ezgi Mar 12 '21 at 10:22
  • A presenter does not mean a view, actually a screen is made up by a presenter and a view. In your case, Screen1 = Screen1View + Screen1Presenter. Screen1View is responsible for describing the elements shown on Screen1, while Screen1Presenter is responsible for controlling how to show the elements. Just copy the whole ChangeColor() to Screen1Presenter, then call presenter->ChangeColor() in buttonCallbackHandler. Please try to know the Model-Presenter-View architecture by yourself. I can't explain to you detail here. – Sean Mar 12 '21 at 12:11
  • If I copy the whole function, it will make no sense to have ChangeColor.cpp. My purpose is to access the view from the outside. Furthermore, I copied the whole func to presenter, presenter couldn't access the box1. – ezgi Mar 12 '21 at 13:30
  • I would like to tell the mechanism of what you want to do: – Sean Mar 12 '21 at 16:00
  • You press the button on screen, the view will receive the click event, in the event handler, the presenter will be called, then the presenter will call the model(your external code), in general, the model will have the data, like your color value. The presenter will get the data value from model, then call the view to change the color. If you still don't know how to code, you should post all for your code, then I may try to modify for you one by one. – Sean Mar 12 '21 at 16:09