从外部访问类对象

问题描述

我在 TouchGFX Designer 上创建了一个基本项目,并使用 touchgfx 库在我自己的 cpp 文件中编写了一个函数。我希望当单击按钮时,该函数从我自己的 cpp 文件调用到 ScreenView.cpp 或 ScreenViewBase.cpp 并更改框的颜色。

这是我的 cpp 文件

#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();
}

这是我想在其中调用我的函数的 Screen1View.cpp。

#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();
    
    }
}

这是声明框的 Screen1BaseView.hpp

#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:

};

我无法访问该框。有办法吗?

解决方法

TouchGFX 使用 MPV 架构,即 Model-Presenter-View。关键是Model和View不能互相访问。

在您的情况下,您的 cpp 文件充当模型的角色,因此不允许访问 View 中的元素。这就是您无法访问的原因。您需要一个 Presenter 来处理连接。

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...