2022-03-20 10:43 AM
I have created 2 screens , screen1 and screen2, and I would like to override 2 virtual functions inside the ModelListener class from Screen1Presenter and Screen2Presenter respectively. However the overridden function from the derived classs (i.e. Screen1Presenter and Screen2Presenter) is never called.
I have no idea how to solve this problem because its a weird scenario and I am unsure what to search to solve this problem...
Model.cpp
void Model::myBtn1Clicked(void){
modelListener->ShowScreen2(); ///called successfully
}
void Model::myBtn2Clicked(void){
modelListener->ShowScreen1(); ///called successfully
}
ModelListener.hpp
#ifndef MODELLISTENER_HPP
#define MODELLISTENER_HPP
#include <gui/model/Model.hpp>
class ModelListener
{
public:
ModelListener() : model(0) {}
virtual ~ModelListener() {}
void bind(Model* m)
{
model = m;
}
virtual void ShowScreen1(){} //// this line s called , instead of the intended Screen1Presenter::ShowScreen1()
virtual void ShowScreen2(){} //// this line is called , instead of the intended Screen2Presenter::ShowScreen2()
protected:
Model* model;
};
#endif // MODELLISTENER_HPP
Screen1Presenter.hpp
#ifndef SCREEN1PRESENTER_HPP
#define SCREEN1PRESENTER_HPP
#include <gui/model/ModelListener.hpp>
#include <mvp/Presenter.hpp>
using namespace touchgfx;
class Screen1View;
class Screen1Presenter : public touchgfx::Presenter, public ModelListener
{
public:
Screen1Presenter(Screen1View& v);
/**
* The activate function is called automatically when this screen is "switched in"
* (ie. made active). Initialization logic can be placed here.
*/
virtual void activate();
/**
* The deactivate function is called automatically when this screen is "switched out"
* (ie. made inactive). Teardown functionality can be placed here.
*/
virtual void deactivate();
virtual ~Screen1Presenter() {};
void myBtnClicked();
virtual void ShowScreen1(void) override;
private:
Screen1Presenter();
Screen1View& view;
};
#endif // SCREEN1PRESENTER_HPP
Screen1Presenter.cpp
#include <gui/screen1_screen/Screen1View.hpp>
#include <gui/screen1_screen/Screen1Presenter.hpp>
Screen1Presenter::Screen1Presenter(Screen1View& v)
: view(v)
{
}
void Screen1Presenter::activate()
{
}
void Screen1Presenter::deactivate()
{
}
void Screen1Presenter::myBtnClicked()
{
model->myBtn1Clicked();
}
void Screen1Presenter::ShowScreen1(void){ //NEVER CALLED
view.ShowScreen();
}
Screen2Presenter.hpp
#ifndef SCREEN2PRESENTER_HPP
#define SCREEN2PRESENTER_HPP
#include <gui/model/ModelListener.hpp>
#include <mvp/Presenter.hpp>
using namespace touchgfx;
class Screen2View;
class Screen2Presenter : public touchgfx::Presenter, public ModelListener
{
public:
Screen2Presenter(Screen2View& v);
/**
* The activate function is called automatically when this screen is "switched in"
* (ie. made active). Initialization logic can be placed here.
*/
virtual void activate();
/**
* The deactivate function is called automatically when this screen is "switched out"
* (ie. made inactive). Teardown functionality can be placed here.
*/
virtual void deactivate();
virtual ~Screen2Presenter() {};
void myBtnClicked();
virtual void ShowScreen2(void) override;
private:
Screen2Presenter();
Screen2View& view;
};
#endif // SCREEN2PRESENTER_HPP
Screen2Presenter.cpp
#include <gui/screen2_screen/Screen2View.hpp>
#include <gui/screen2_screen/Screen2Presenter.hpp>
Screen2Presenter::Screen2Presenter(Screen2View& v)
: view(v)
{
}
void Screen2Presenter::activate()
{
}
void Screen2Presenter::deactivate()
{
}
void Screen2Presenter::myBtnClicked()
{
model->myBtn2Clicked();
}
void Screen2Presenter::ShowScreen2(void){ //NEVER CALLED
view.ShowScreen();
}
I believe i've posted the relevant codes. Please let me know if anything need clarification
Update 1:
I made a cheap work-around by making all the functions static... really hoped the virtual function overload could work...
Model.cpp
#include <gui/model/Model.hpp>
#include <gui/model/ModelListener.hpp>
#include <gui/screen1_screen/Screen1Presenter.hpp> //added
#include <gui/screen2_screen/Screen2Presenter.hpp> //added
Model::Model() : modelListener(0)
{
}
void Model::tick()
{
}
void Model::myBtn1Clicked(void){
Screen2Presenter::ShowScreen2(); //direct call
}
void Model::myBtn2Clicked(void){
Screen1Presenter::ShowScreen1(); //direct call
}
ModelListener.hpp
#ifndef MODELLISTENER_HPP
#define MODELLISTENER_HPP
#include <gui/model/Model.hpp>
class ModelListener
{
public:
ModelListener() : model(0) {}
virtual ~ModelListener() {}
void bind(Model* m)
{
model = m;
}
//virtual void ShowScreen2(){} // REMOVED
//virtual void ShowScreen1(){} // REMOVED
protected:
Model* model;
};
#endif // MODELLISTENER_HPP
Screen1Presenter.hpp
public:
static void ShowScreen1(void);
Screen1Presenter.cpp
void Screen1Presenter::ShowScreen1(void){
Screen1View::ShowScreen();
}
Screen1View.hpp
public:
static void ShowScreen(void);
Screen1View.cpp
void Screen1View::ShowScreen(void){
static_cast<FrontendApplication*>(Application::getInstance())->gotoScreen1ScreenWipeTransitionWest();
}
2024-03-06 01:08 PM
Hello,
Looks like you didn't get any response to this problem. I'm having the exact same issue. Were you able to resolve it with any other method than calling the functions directly?
Thanks,
Doug
2024-03-07 12:21 AM
Hello
I think the initial post first trial works if just change
virtual void ShowScreen1(void) override;
To
void ShowScreen1(void);
In presenter.hpp. Then it should override the function.
Maybe my example project in this thread helps.
Br JTP