gpt4 book ai didi

c++ - wxwidgets连接到成员类中的函数

转载 作者:行者123 更新时间:2023-11-28 06:28:59 25 4
gpt4 key购买 nike

标题:我上了带有测试功能和面板的类(class)

class testsubclass{
public:
testsubclass();
void testfunc(wxCommandEvent &event);
};

class panelonwindow : public wxPanel{
public:
panelonwindow(wxWindow *windowid, int ID);
wxWindow *mywindow, *mypanel;
wxTextCtrl *mytextcontrol;

void maketextctrl(std::string label, int &id);
}

我希望类(class)在我的主窗口上创建一个 Textcontrol。作为我使用的函数

testsubclass::testsubclass(){
}

panelonwindow::panelonwindow(wxWindow *windowid, int ID)
:wxPanel(windowid, ID, wxDefaultPosition, wxSize(150, 150)){
mywindow = windowid;
mypanel = this;
};

void panelonwindow::maketextctrl(std::string label, int &id){
wxString newlabel(label.c_str(), wxConvUTF8);
mytextcontrol = new wxTextCtrl(mypanel, id, newlabel, wxDefaultPosition, wxSize(130, 30));
}


void testsubclass::testfunc(wxCommandEvent &event){
printf("%s\n", "testfunc was called");
}

我的主窗口头文件包含指向这两个类的指针:

标题:

wxWindow *mainwindow;
testsubclass *mysubclass;
panelonwindow *testpanel;
int ID1 = 100;
int ID2 = 101;

现在主函数看起来像这样:

mainwindow = this;
std::string textcontrolstring = "this is a test";
testpanel = new panelonwindow(mainwindow, ID);
testpanel->maketextctrl(textcontrolstring, ID2);

mysubclass = new testsubclass();

问题是,我无法将 testsublass 函数 testfunc 链接到主窗口函数中的这样一个事件,当我尝试执行如下操作时,我收到了一些神秘的编译器消息:

Connect(ID2, wxEVT_COMMAND_TEXT_UPDATED,
wxCommandEventHandler(mysubclass->testfunc));

我可以将 void panelonwindow::maketextctrl 中的事件链接到 panelonwindow 函数的另一个成员(前提是我以类似 void panelonwindow::testfunc(wxCommandEvent &event) 的方式声明它们

void panelonwindow::maketextctrl(std::string label, int &id){
wxString newlabel(label.c_str(), wxConvUTF8);
mytextcontrol = new wxTextCtrl(mypanel, id, newlabel, wxDefaultPosition, wxSize(130, 30));

Connect(id, wxEVT_COMMAND_TEXT_UPDATED,
CommandEventHandler(panelonwindow::testfunc))
}

因为我计划在面板上创建很多按钮,所以我更喜欢在成员类中定义函数,而不是为每个按钮/textcontrolwindow 单独编写一个函数来控制所有窗口。

这可能是一个新手问题,但我将不胜感激任何形式的帮助

最佳答案

您需要在生成事件的对象上调用 Connect() 并将处理它的对象传递给它(否则 wxWidgets 将如何确定将事件发送到哪个对象?它不能'不要阅读你的代码来找出答案)。因此,要在 testsubclass::testfunc() 中处理 evnet,您需要调用

Connect(id, wxEVT_COMMAND_TEXT_UPDATED, wxCommandEventHandler(testsubclass::testfunc), NULL, mysubclass);

但您仍然需要决定您想要/需要调用哪个对象 Connect()

如果你使用 wxWidgets 3(你应该),考虑使用更短的事件类型名称和更现代的 Bind() 因为它更清晰和更短:

Bind(wxEVT_TEXT, &testsubclass::testfunc, mysubclass, id);

关于c++ - wxwidgets连接到成员类中的函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27991712/

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