我有一个不同宽度的圆圈。我创建了一个由Screen1ViewBase类继承的子类,用于访问WidthChange.cpp中的"circlewidth“。我可以访问"circlewidth“。但是我不能在Screen1View.cpp中调用Change函数,因为它不是静态的。我将Change函数设为静态的,但这一次我无法访问"circlewidth“和Screen1View的另一个成员。
#ifndef SCREEN1VIEWBASE_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/canvas/Circle.hpp>
#include <touchgfx/widgets/canvas/PainterRGB565.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::Circle circlewidth;
touchgfx::PainterRGB565 circlewidthPainter;
private:
/*
* Canvas Buffer Size
*/
static const uint16_t CANVAS_BUFFER_SIZE = 7200;
uint8_t canvasBuffer[CANVAS_BUFFER_SIZE];
};
#endif // SCREEN1VIEWBASE_HPP#ifndef SCREEN1VIEW_HPP
#define SCREEN1VIEW_HPP
#include <gui_generated/screen1_screen/Screen1ViewBase.hpp>
#include <gui/screen1_screen/Screen1Presenter.hpp>
class Screen1View : public Screen1ViewBase
{
public:
Screen1View();
virtual ~Screen1View() {}
virtual void setupScreen();
virtual void tearDownScreen();
virtual void handleTickEvent();
protected:
int lineWidthLimit;
float circleWidthChangeFactor;
};
#endif // SCREEN1VIEW_HPP#include <gui/screen1_screen/Screen1View.hpp>
#include <gui/WidthChange.hpp>
Screen1View::Screen1View()
{
}
void Screen1View::setupScreen()
{
Screen1ViewBase::setupScreen();
}
void Screen1View::tearDownScreen()
{
Screen1ViewBase::tearDownScreen();
}
void Screen1View::handleTickEvent()
{
WidthChange::Change();
}这是我的类声明
#ifndef WIDTHCHANGE_HPP
#define WIDTHCHANGE_HPP
#include <gui/screen1_screen/Screen1View.hpp>
#include <gui_generated/screen1_screen/Screen1ViewBase.hpp>
class WidthChange : public Screen1View
{
public:
static void Change();
};
#endif这是声明Change()函数的地方
#include <gui/WidthChange.hpp>
#include <gui_generated/screen1_screen/Screen1ViewBase.hpp>
#include <gui/screen1_screen/Screen1View.hpp>
#include <gui/screen1_screen/Screen1Presenter.hpp>
void WidthChange::Change()
{
float currentRad;
float lineWidth;
circlewidth.invalidate();
circlewidth.getRadius(currentRad);
if (currentRad >= lineWidthLimit)
{
circleWidthChangeFactor = -0.25;
}
else if (currentRad<= 15)
{
circleWidthChangeFactor = 0.25;
}
currentRad = currentRad + circleWidthChangeFactor;
circlewidth.setRadius(currentRad);
circlewidth.getLineWidth(lineWidth);
circlewidth.setLineWidth(lineWidth + 2 * (circleWidthChangeFactor * (-1)));
circlewidth.invalidate();
}我知道有很多关于它的事情,但我想知道是否有一种方法可以在调用Change函数时访问整个Screen1View成员和Screen1ViewBase成员。
发布于 2021-03-27 09:00:35
c++中的静态成员函数只能访问静态成员数据或其他静态成员函数。Screen1View可以访问静态函数。
如果你的回调需要做一些复杂的ui设置/计算,那么像这样的中间类可能是个好主意。但是在这里创建Screen1View的子类是没有意义的,因为它的类型是View,这不是您想要的(也会带来很多负担)。
如果您需要访问Screen1View中的成员,可以使用非静态成员定义一个类,并使用指向父成员的指针来构造它,而不是使用继承,但是并不建议为成员提供对其父成员的那种访问。
相反,我建议在类定义中使用更通用的函数。例如(人为设计的例子)。这将允许您在视图回调处理程序中保持对代码的清晰描述,而不是将其全部隐藏在helperclass中的某个函数中。这些函数可以是静态的。
void HelperClass::setCircleRadius(touchgfx::Circle* circle, float radius)
{
circle.setRadius(radius);
circle.invalidate();
}如果我们现在假装CircleHelper更完整,那么视图中的处理程序代码将如下所示,并且更具可读性:
void Screen1View::someCallback()
{
CircleHelper::setRadius(&myCircle, someValue);
//More calls here to do what you want. LineHelper::..
//But now the code communicates only what it needs to.
//Most often "invalidate" is expected, so we don't want to be
//reading 100 occurrences of invalidate(); clutter
}在TouchGFX设计器为您生成ui代码之前,我在很大程度上依赖于静态助手类来进行繁琐的设置,这些设置通常只会弄乱setupScreen(),并且不会向任何人提供任何有价值的信息。
https://stackoverflow.com/questions/66783665
复制相似问题