WxWidgets - 从主文件以外的文件更改文本框

时间:2011-11-24 04:52:56

标签: c++ c class wxwidgets codeblocks

注意:我完全修改了问题并将其转换为专门针对此问题的示例项目,因此Nicks的回答不再有意义。 wxQuestionMain.h和wxQuestionMain.cpp是经过温和修改的wxWidget文件,由Code :: Blocks自动生成。

go

当我点击“Go”按钮时,我希望“wxQuestionMain.cpp”中的按钮事件调用“someFile(”中的“somefunction()”。这很好用。但是我想从“somefunction()”里面改变文本框“txtCtrl1”中的文本,这是行不通的,因为“somefunction()”不是wxWidget类的一部分,我不希望它是。 wxwidget类在“wxQuestionMain.h”中创建。

wxQuestionMain.h - >只需创建类

#ifndef WXQUESTIONMAIN_H
#define WXQUESTIONMAIN_H
#define BOOST_FILESYSTEM_VERSION 2

#ifndef WX_PRECOMP
    #include <wx/wx.h>
#endif

#include "wxQuestionApp.h"


#include <wx/button.h>
#include <wx/statline.h>
class wxQuestionDialog: public wxDialog
{
    public:
    wxQuestionDialog(wxDialog *dlg, const wxString& title);
    ~wxQuestionDialog();

    protected:
    enum
    {
        idBtnGo = 1000
    };
    wxStaticText* m_staticText1;
    wxStaticLine* m_staticline1;
    wxButton* BtnGo;
    wxTextCtrl* textCtrl1;

    private:
    void OnClose(wxCloseEvent& event);
    void OnGo(wxCommandEvent& event);
    DECLARE_EVENT_TABLE()
};

void somefunction();

#endif // WXQUESTIONMAIN_H

wxQuestionMain.cpp - &gt;很多yadda yadda,然后是最底层的处理buttonclick事件的函数。

#ifdef WX_PRECOMP
#include "wx_pch.h"
#endif

#ifdef __BORLANDC__
#pragma hdrstop
#endif //__BORLANDC__

#include "wxQuestionMain.h"

//helper functions
enum wxbuildinfoformat {
    short_f, long_f };

wxString wxbuildinfo(wxbuildinfoformat format)
{
    wxString wxbuild(wxVERSION_STRING);

    if (format == long_f )
    {
#if defined(__WXMSW__)
    wxbuild << _T("-Windows");
#elif defined(__WXMAC__)
    wxbuild << _T("-Mac");
#elif defined(__UNIX__)
    wxbuild << _T("-Linux");
#endif

#if wxUSE_UNICODE
    wxbuild << _T("-Unicode build");
#else
    wxbuild << _T("-ANSI build");
#endif // wxUSE_UNICODE
    }

    return wxbuild;
}


BEGIN_EVENT_TABLE(wxQuestionDialog, wxDialog)
    EVT_CLOSE(wxQuestionDialog::OnClose)
    EVT_BUTTON(idBtnGo, wxQuestionDialog::OnGo)
END_EVENT_TABLE()

wxQuestionDialog::wxQuestionDialog(wxDialog *dlg, const wxString &title)
    : wxDialog(dlg, -1, title)
{
    this->SetSizeHints(wxDefaultSize, wxDefaultSize);
    wxBoxSizer* bSizer1;
    bSizer1 = new wxBoxSizer(wxHORIZONTAL);
    m_staticText1 = new wxStaticText(this, wxID_ANY, wxT("Welcome To\nwxWidgets"), wxDefaultPosition, wxDefaultSize, 0);
    m_staticText1->SetFont(wxFont(20, 74, 90, 90, false, wxT("Arial")));
    bSizer1->Add(m_staticText1, 0, wxALL|wxEXPAND, 5);
    wxBoxSizer* bSizer2;
    bSizer2 = new wxBoxSizer(wxVERTICAL);

    wxPoint textCtrl1Position(5,5); //Position
    wxSize textCtrl1size(120,25); //Size
    textCtrl1 = new wxTextCtrl(this, wxID_ANY, "hi", wxDefaultPosition, wxDefaultSize, 0, wxDefaultValidator, "textCtrl1"); //Create textCtrl
    bSizer2->Add(textCtrl1, 0, wxALL|wxEXPAND, 5); //Add to sizer

    m_staticline1 = new wxStaticLine(this, wxID_ANY, wxDefaultPosition, wxDefaultSize, wxLI_HORIZONTAL);
    bSizer2->Add(m_staticline1, 0, wxALL|wxEXPAND, 5);
    BtnGo = new wxButton(this, idBtnGo, wxT("&Go"), wxDefaultPosition, wxDefaultSize, 0);
    bSizer2->Add(BtnGo, 0, wxALL, 5);
    bSizer1->Add(bSizer2, 1, wxEXPAND, 5);
    this->SetSizer(bSizer1);
    this->Layout();
    bSizer1->Fit(this);
}


wxQuestionDialog::~wxQuestionDialog()
{
}

void wxQuestionDialog::OnClose(wxCloseEvent &event)
{
    Destroy();
}

void wxQuestionDialog::OnGo(wxCommandEvent &event)
{
    somefunction();
}

otherFile.cpp:

#include "wxQuestionMain.h"

void somefunction()
{
    //Try to change the text in textCtrl1
    wxQuestionDialog::textCtrl1->AppendText("Red text\n");
}

产地:

error: ‘wxTextCtrl* wxQuestionDialog::textCtrl1’ is protected
error: invalid use of non-static data member ‘wxQuestionDialog::textCtrl1’

所以我移动'wxTextCtrl * textCtrl1;'在'wxQuestionMain.h'中从'protected'到'public'

产地:

error: invalid use of non-static data member ‘wxQuestionDialog::textCtrl1’

wxQuestionMain.h中的类似乎是sais'class wxQuestionDialog:public wxDialog'

我不知道那个“公共”部分是什么意思,我从来没有见过像以前那样创建过类,但我会尝试改变'otherFile.cpp'所以它是wisDialog而不是wxQuestionDialog

#include "wxQuestionMain.h"

void somefunction()
{
    //Try to change the text in textCtrl1
    wxDialog::textCtrl1->AppendText("Red text\n");
}

产地:

error: ‘textCtrl1’ is not a member of ‘wxDialog’

我在这里不知所措..如何更新“textCtrl1”中的文本而不向wxWidget类添加“somefunction()”?

CodeBlocks自动生成了2个其他文件,不确定它们是否重要,但在这里它们是。

wxQuestionApp.cpp

#ifdef WX_PRECOMP
#include "wx_pch.h"
#endif

#ifdef __BORLANDC__
#pragma hdrstop
#endif //__BORLANDC__

#include "wxQuestionApp.h"
#include "wxQuestionMain.h"

IMPLEMENT_APP(wxQuestionApp);

bool wxQuestionApp::OnInit()
{

    wxQuestionDialog* dlg = new wxQuestionDialog(0L, _("wxWidgets Application Template"));

    dlg->Show();
    return true;
}

wxQuestionApp.h

#ifndef WXQUESTIONAPP_H
#define WXQUESTIONAPP_H

#include <wx/app.h>

class wxQuestionApp : public wxApp
{
    public:
    virtual bool OnInit();
};

#endif // WXQUESTIONAPP_H

1 个答案:

答案 0 :(得分:1)

addtolistbox2gfxDialog的私有成员方法 - 即使你 有正确构造的对象,你也无法从外部调用该方法gfxDialog个实例。在至少,你需要从addtolistbox2移动public:private:,然后在正确构造的实例上调用它(构造函数需要参数但你的代码没有提供):

gfxDialog testtime(0, "test");
testtime.addtolistbox2("somestring");

(唯一有效的构造函数需要父对话框和标题字符串:

class gfxDialog: public wxDialog
{
    public:
    gfxDialog(wxDialog *dlg, const wxString& title);

如果我正确地记住了我的wxWidgets,那么父级可能是NULL,但当然你仍然需要提供参数)