访问自定义wxPanel中的子项
Access child in customized wxPanel
所以我有一个从wxPanel继承的类MyPanel:
class MyPanel : public wxPanel
{
public:
MyPanel(wxWindow* parent) : wxPanel(parent){}
void OnMouseMove(wxMouseEvent& event);
private:
DECLAER_EVENT_TABLE()
};
以及下面定义的另一个主要wxframe:
class mainFrame : public wxFrame
{
...
private:
...
MyPanel* myPanel;
...
wxStaticText* StaticText1;
...
};
StaticText1最终将被分配为myPanel的子级。所以我想在方法OnMouseMove()中更新鼠标光标的坐标
我想知道如何访问StaticText1并更新内容。
使用友元声明,如下所示:
class mainFrame : public wxFrame
{
friend class MyPanel;
...
};
您可以在MyPanel中定义一个成员,并在需要时将StaticText1分配给它。在OnMouseMove检查中,如果StaticText1是否为NULL。
class MyPanel : public wxPanel
{
public:
MyPanel(wxWindow* parent) : wxPanel(parent){}
void OnMouseMove(wxMouseEvent& event) { if (StaticText1) { /* do something */ } ;
void SetStaticText1(wxStaticText* txt) { StaticText1 = txt;}
private:
DECLAER_EVENT_TABLE()
wxStaticText* StaticText1;
};
从编程的角度来看,您可以使用朋友解决方案。然而,从逻辑上讲,如果wxStaticText*StaticText1将由MyPanel作为父级,并且主要从MyPanel的一个成员函数中引用/更新。那么wxStaticText最好在MyPanel中声明和定义,而不是在mainFrame中。
相关文章:
- C++映射:具有自定义类的运算符[]不起作用(总是返回0)
- 如何将点击的信号和插槽添加到qt中的自定义按钮中
- C++自定义比较函数
- 如何比较自定义类的std::变体
- std::设置自定义比较器
- 如何正确实现和访问运算符的各种自定义枚举器
- flutter:即使shouldRepaint()返回true,自定义画家也不会重新绘制
- 自定义先决条件对移动分配运算符有效吗
- 使用VS Code和CMake Tools运行自定义命令
- 如何创建从Maya(或类似程序)到虚幻引擎的自定义数据导出插件
- std::ranges::elements_view,用于自定义类似元组的数据
- 跟随整数索引列表的自定义类迭代器
- 参数化自定义CMake工具链
- 使用自定义比较函数使用std::sort()对矢量字符串进行排序时出现问题
- 如何在自定义类中启用'auto loops'?
- 使用QJsEngine在Qt中注册自定义类型
- Qt自定义QPush按钮未显示在布局上
- 自定义对象的dlib序列化在gcc中失败
- 访问自定义wxPanel中的子项
- 从自定义wxPanel wxWidgets访问另一个元素