在构造函数中使用函数connect()

时间:2014-07-19 11:42:29

标签: c++ events opengl wxwidgets

我不理解在某些类的构造函数中使用函数connect()。我认为这是为了连接"我的程序的图形部分的事件,但如果我不在构造函数中使用任何连接函数,它会使我一样。以下是我的代码的一部分,例如:

#include "VueOpenGL.h"
#include "wx/wx.h"
#include "wx/glcanvas.h"
#include "wx/progdlg.h"
using namespace std;

//Constructor of the class "VueOpenGL"

VueOpenGL::VueOpenGL(wxWindow* parent, wxSize const& taille, wxPoint const& position)
:wxGLCanvas(parent, wxID_ANY, position, taille,
 wxSUNKEN_BORDER|wxFULL_REPAINT_ON_RESIZE|WX_GL_DOUBLEBUFFER)
{
  //Events
Connect(wxEVT_KEY_DOWN, wxKeyEventHandler(VueOpenGL::OnKeyDown));
} 

...

void VueOpenGL::OnKeyDown(wxKeyEvent& event) {
    switch(event.GetKeyCode()) {
        case WXK_LEFT:
             instructions_1;
        break;
        case WXK_RIGHT:
             instructions_2;
        break;
     }
}

(所有原型都在VueOpenGL.h中)

1 个答案:

答案 0 :(得分:1)

你没有向我们展示一切。如果没有Connect(wxEVT_KEY_DOWN),则按键事件将不会传递给您的处理程序。因此,如果它仍然被调用,它必须以其他方式连接,或者您可能只是没有测试您认为正在测试的代码(例如,在注释掉包含Connect()的行后重建失败并且您仍在运行旧的版本)。