gpt4 book ai didi

c++ - 如何通过 wxTheApp->GetTopWindow() 获取从 wxFrame 派生的 My 对象?

转载 作者:行者123 更新时间:2023-11-30 03:59:15 26 4
gpt4 key购买 nike

我不太擅长 C++ 中的 OOP 规则。我有这个应用程序 OnInit() 回调:

bool Application::OnInit()
{
MainFrame *mainFrame = new MainFrame();

mainFrame->Show();
SetTopWindow(mainFrame);

return true;
}

在从 wxFrame 派生的 MainFrame 中,我有私有(private)成员和它的 getter:

wxDialog* MainFrame::GetConfigDialog()
{
return m_ConfigDialog;
}

我想在其他类中获取ConfigDialog,所以我调用

wxTheApp->GetTopWindow()->GetConfigDialog()

但它提示 wxWindow 没有名为 GetConfigDialog() 的成员。我能以某种方式从 wxTheApp 获取我的 MainFrame 吗?

最佳答案

这里有两件事。首先,您需要使用 wxGetApp()功能。与 wxTheApp 不同,它返回派生类型的应用程序,即 Application 而不是 wxApp 在您的情况下(并且它还返回它作为引用而不是指针,因为它永远不应该为空)。请注意,如文档中所述,您需要使用 wxDECLARE_APP() 宏在包含应用程序类声明的 header 中声明此函数。

其次,您仍然不能通过wxWindow 指针调用派生类GetConfigDialog() 方法。您可以使用动态转换,但这会很丑陋且不安全。相反,添加一个方法来检索真正的主框架到您的应用程序类(当然,您也需要在其中存储一个指向它的指针),例如GetMainFrame()

如果你同时做这两件事,你的代码会变成

wxGetApp().GetMainFrame()->GetConfigDisplay()

关于c++ - 如何通过 wxTheApp->GetTopWindow() 获取从 wxFrame 派生的 My 对象?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26998312/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com