簡體   English   中英

從外部訪問 class object

[英]Accessing a class object from outside

我在 TouchGFX Designer 上創建了一個基本項目,並使用 touchgfx 庫在我自己的 cpp 文件中編寫了 function。 我希望當單擊按鈕時,從我自己的 cpp 文件中將 function 調用到 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();
}

這是我想調用我的 function 的 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 文件扮演 model 的角色,因此不允許訪問視圖中的元素。 這就是你無法訪問的原因。 您需要一個 Presenter 來處理連接。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM