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

use of function connect() in the constructor

本文关键字:函数 connect 构造函数      更新时间:2023-10-16

我不明白在某些类的构造函数中使用函数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)

你没有把一切都告诉我们。如果没有Connect(wxEVT_KEY_DOWN),按键事件就不会被传递给你的处理器。因此,如果它仍然被调用,它必须以其他方式连接,或者也许你只是没有测试你认为你正在测试的代码(例如,在注释掉包含Connect()失败的行后重建,你仍然运行旧版本)。