wxGauge 是构造函数之外的 nullptr

wxGauge was nullptr out of constructor

首先对不起我的英语,它不是我的母语

我是 wxWidgets for c++ 的初学者,我开始开发一个带有进度条和一些后台进程的简单应用程序

不幸的是,当我调用 wx_MyGauge->SetValue(20) 时,出现异常“wx_MyGauge was nullptr”

这只发生在构造函数之外,如果我从构造函数调用该函数,效果很好

我做错了什么?

//header

#pragma once
#include "wx/wx.h"

class testGauge : public wxFrame
{
public:
    testGauge();
    ~testGauge();

    wxGauge* wx_MyGauge;
    wxPanel* pnl;

    void Importar(wxMouseEvent& event);
};

//cpp

#include "testGauge.h"
testGauge::testGauge() : wxFrame(nullptr, wxID_ANY, "blabla", wxDefaultPosition, wxSize(500, 400))
{
    pnl = new wxPanel(this, wxID_ANY, wxPoint(10, 10), wxSize(500, 400));

    wx_MyGauge= new wxGauge(pnl, 1001, 100, wxPoint(10,10), wxSize(100,20), wxGA_HORIZONTAL);
    wx_MyGauge->SetValue(10);// here OK

    wxButton* btn = new wxButton(pnl,wxID_ANY,"Teste",wxPoint(10,30),wxSize(50,30));
    btn->Connect(wxEVT_LEFT_UP, wxMouseEventHandler(testGauge::Importar));
    

    this->Layout();
    this->Centre(wxBOTH);
}


void testGauge::Importar(wxMouseEvent& WXUNUSED(event))
{
    testGauge::wx_MyGauge->SetValue(20);// here Error 
}

testGauge::~testGauge()
{

}

致力于 windows 10,visual studio 2019,wxWidgets 3.1.5

您调用 pnl 的 Connect 方法,但没有传递 Importar 是哪个事件处理程序。因此,方法 Connect 将鼠标事件加入 pnl 而不是 testGuage

btn->Connect(wxEVT_LEFT_UP, wxMouseEventHandler(testGauge::Importar));

必须

btn->Connect(wxEVT_LEFT_UP, wxMouseEventHandler(testGauge::Importar), nullptr, this);