【问题标题】:wxwidgets connect to function in member classwxwidgets 连接到成员类中的函数
【发布时间】:2015-01-16 19:50:37
【问题描述】:

标题: 我有一个带有 testfunction 和一个面板的课程

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);
}

我希望班级在我的主窗口上创建一个 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))
}

由于我计划在面板上创建大量按钮,因此我更愿意在成员类中定义函数,而不是为每个按钮/文本控件窗口编写一个函数,分别控制所有窗口。

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

【问题讨论】:

    标签: c++ wxwidgets


    【解决方案1】:

    您需要在生成事件的对象上调用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);
    

    【讨论】:

      【解决方案2】:

      这在很大程度上解决了它。但是,部分

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

      还是不行。我用

      代替了它
      Connect(id, wxEVT_COMMAND_TEXT_UPDATED,
      (wxObjectEventFunction) (wxEventFunction) (wxCommandEventFunction) &testsubclass::testfunc,
      NULL, mysubclass)
      

      修复了它。

      【讨论】:

        猜你喜欢
        • 2012-03-15
        • 1970-01-01
        • 2012-10-08
        • 1970-01-01
        • 2022-01-10
        • 2011-04-06
        • 2019-10-16
        • 2020-12-04
        • 2020-09-29
        相关资源
        最近更新 更多