【问题标题】:How to correctly show a wxMessageBox with the value of "string01"?如何正确显示值为“string01”的 wxMessageBox?
【发布时间】:2020-06-11 03:35:50
【问题描述】:

如何正确显示值为“string01”的MessageBox?

gui.h:

///////////////////////////////////////////////////////////////////////////
// C++ code generated with wxFormBuilder (version Oct 26 2018)
// http://www.wxformbuilder.org/
//
// PLEASE DO *NOT* EDIT THIS FILE!
///////////////////////////////////////////////////////////////////////////

#pragma once

#include <wx/artprov.h>
#include <wx/xrc/xmlres.h>
#include <wx/intl.h>
#include <wx/string.h>
#include <wx/stattext.h>
#include <wx/gdicmn.h>
#include <wx/font.h>
#include <wx/colour.h>
#include <wx/settings.h>
#include <wx/textctrl.h>
#include <wx/sizer.h>
#include <wx/statline.h>
#include <wx/bitmap.h>
#include <wx/image.h>
#include <wx/icon.h>
#include <wx/button.h>
#include <wx/dialog.h>
#include <wx/msgdlg.h> 

///////////////////////////////////////////////////////////////////////////


///////////////////////////////////////////////////////////////////////////////
/// Class MainDialogBase
///////////////////////////////////////////////////////////////////////////////
class MainDialogBase : public wxDialog
{
    private:

    protected:
        wxStaticText* m_staticText3;
        wxStaticText* m_staticText1;
        wxTextCtrl* m_textCtrl1;
        wxStaticText* m_staticText4;
        wxTextCtrl* m_textCtrl2;
        wxStaticLine* m_staticLine;
        wxButton* m_button1;
        wxButton* m_button2;

        // Virtual event handlers, overide them in your derived class
        virtual void OnCloseDialog( wxCloseEvent& event ) { event.Skip(); }
        virtual void m_button1OnButtonClick( wxCommandEvent& event ) { event.Skip(); }
        virtual void m_button2OnButtonClick( wxCommandEvent& event ) { event.Skip(); }


    public:

        MainDialogBase( wxWindow* parent, wxWindowID id = wxID_ANY, const wxString& title = wxEmptyString, const wxPoint& pos = wxDefaultPosition, const wxSize& size =     wxSize( 191,252 ), long style = wxCLOSE_BOX|wxDEFAULT_DIALOG_STYLE );
        ~MainDialogBase();

};

gui.cpp:

///////////////////////////////////////////////////////////////////////////
// C++ code generated with wxFormBuilder (version Oct 26 2018)
// http://www.wxformbuilder.org/
//
// PLEASE DO *NOT* EDIT THIS FILE!
///////////////////////////////////////////////////////////////////////////

#include "gui.h"

///////////////////////////////////////////////////////////////////////////

MainDialogBase::MainDialogBase( wxWindow* parent, wxWindowID id, const wxString& title, const wxPoint& pos, const wxSize& size, long style ) : wxDialog( parent, id, title, pos, size, style )
{
    this->SetSizeHints( wxSize( -1,-1 ), wxDefaultSize );

    wxBoxSizer* mainSizer;
    mainSizer = new wxBoxSizer( wxVERTICAL );

    wxBoxSizer* bSizer8;
    bSizer8 = new wxBoxSizer( wxHORIZONTAL );

    wxBoxSizer* bSizer2;
    bSizer2 = new wxBoxSizer( wxVERTICAL );

    m_staticText3 = new wxStaticText( this, wxID_ANY, _("Patient's data"), wxDefaultPosition, wxSize( 105,-1 ), 0 );
    m_staticText3->Wrap( -1 );
    m_staticText3->SetFont( wxFont( 10, wxFONTFAMILY_SWISS, wxFONTSTYLE_NORMAL, wxFONTWEIGHT_BOLD, false, wxT("Sans") ) );

    bSizer2->Add( m_staticText3, 0, wxALL, 5 );

    wxBoxSizer* bSizer3;
    bSizer3 = new wxBoxSizer( wxHORIZONTAL );

    m_staticText1 = new wxStaticText( this, wxID_ANY, _("First Name:"), wxDefaultPosition, wxDefaultSize, 0 );
    m_staticText1->Wrap( -1 );
    bSizer3->Add( m_staticText1, 0, wxALL, 5 );

    m_textCtrl1 = new wxTextCtrl( this, wxID_ANY, wxEmptyString, wxDefaultPosition, wxDefaultSize, 0 );
    bSizer3->Add( m_textCtrl1, 0, wxALL, 5 );


    bSizer2->Add( bSizer3, 1, wxEXPAND, 5 );

    wxBoxSizer* bSizer4;
    bSizer4 = new wxBoxSizer( wxHORIZONTAL );

    m_staticText4 = new wxStaticText( this, wxID_ANY, _("Last Name:"), wxDefaultPosition, wxDefaultSize, 0 );
    m_staticText4->Wrap( -1 );
    bSizer4->Add( m_staticText4, 0, wxALL, 5 );

    m_textCtrl2 = new wxTextCtrl( this, wxID_ANY, wxEmptyString, wxDefaultPosition, wxDefaultSize, 0 );
    bSizer4->Add( m_textCtrl2, 0, wxALL, 5 );


    bSizer2->Add( bSizer4, 1, wxEXPAND, 5 );


    bSizer8->Add( bSizer2, 1, 0, 5 );


    mainSizer->Add( bSizer8, 1, wxEXPAND, 5 );

    m_staticLine = new wxStaticLine( this, wxID_ANY, wxDefaultPosition, wxDefaultSize, wxLI_HORIZONTAL );
    mainSizer->Add( m_staticLine, 0, wxEXPAND | wxALL, 5 );

    m_button1 = new wxButton( this, wxID_ANY, _("Record Data"), wxDefaultPosition, wxDefaultSize, 0 );
    mainSizer->Add( m_button1, 0, wxALL|wxEXPAND, 5 );

    m_button2 = new wxButton( this, wxID_ANY, _("List"), wxDefaultPosition, wxDefaultSize, 0 );
    mainSizer->Add( m_button2, 0, wxALL|wxEXPAND, 5 );

    wxBoxSizer* bSizer9;
    bSizer9 = new wxBoxSizer( wxHORIZONTAL );


    mainSizer->Add( bSizer9, 1, wxEXPAND, 5 );


    this->SetSizer( mainSizer );
    this->Layout();

    this->Centre( wxBOTH );

    // Connect Events
    this->Connect( wxEVT_CLOSE_WINDOW, wxCloseEventHandler( MainDialogBase::OnCloseDialog ) );
    m_button1->Connect( wxEVT_COMMAND_BUTTON_CLICKED, wxCommandEventHandler( MainDialogBase::m_button1OnButtonClick ), NULL, this );
    m_button2->Connect( wxEVT_COMMAND_BUTTON_CLICKED, wxCommandEventHandler( MainDialogBase::m_button2OnButtonClick ), NULL, this );
}

MainDialogBase::~MainDialogBase()
{
    // Disconnect Events
    this->Disconnect( wxEVT_CLOSE_WINDOW, wxCloseEventHandler( MainDialogBase::OnCloseDialog ) );
    m_button1->Disconnect( wxEVT_COMMAND_BUTTON_CLICKED, wxCommandEventHandler( MainDialogBase::m_button1OnButtonClick ), NULL, this );
    m_button2->Disconnect( wxEVT_COMMAND_BUTTON_CLICKED, wxCommandEventHandler( MainDialogBase::m_button2OnButtonClick ), NULL, this );

}

继承的gui.h:

#ifndef __inheritedgui__
#define __inheritedgui__

/**
@file
Subclass of MainDialogBase, which is generated by wxFormBuilder.
*/

#include "gui.h"

//// end generated include

/** Implementing MainDialogBase */
class inheritedgui : public MainDialogBase
{
    protected:
        // Handlers for MainDialogBase events.
        void OnCloseDialog( wxCloseEvent& event );
        void m_button1OnButtonClick(wxCommandEvent& event) override;
        void m_button2OnButtonClick(wxCommandEvent& event) override;
    public:
        /** Constructor */
        inheritedgui( wxWindow* parent );
    //// end generated class members

};

#endif // __inheritedgui__

inheritedgui.cpp:

#include "inheritedgui.h"

inheritedgui::inheritedgui( wxWindow* parent )
:
MainDialogBase( parent )
{   

}
wxString string01;

void inheritedgui::m_button1OnButtonClick(wxCommandEvent& event)
{

string01 = m_textCtrl1->GetValue().ToStdString();

}

void inheritedgui::m_button2OnButtonClick(wxCommandEvent& event)
{

wxMessageBox( string01, wxT("This is the title"), wxICON_INFORMATION );

}

void inheritedgui::OnCloseDialog( wxCloseEvent& event )
{
// TODO: Implement OnCloseDialog
}

我试图在 wxMessageBox 中显示“string01”的值(如“inheritedgui.cpp”中所示),但是当我单击“m_button2”时,没有任何反应,我不知道为什么:/

https://i.postimg.cc/jd6WDc6z/Screenshot-20200227-040106.png

当我单击“m_button2”(是显示“列表”的那个)时,我需要做哪些更改才能显示 wxMessageBox?

【问题讨论】:

    标签: c++ wxwidgets wxformbuilder


    【解决方案1】:

    您在一种方法 (m_button1OnButtonClick) 中声明 std::string string01 并尝试在另一种方法 (m_button2OnButtonClick) 中使用它。

    为了能够在您尝试的其他方法中使用它,请将std::string string01 设为class inheritedgui 上的成员变量。

    然后当你打电话时:

    wxMessageBox(wxString(string01.c_str(), wxConvUTF8), wxT("This is the title"), wxICON_INFORMATION);
    

    另外,是否特别需要使用std::string 而不是wxString?如果没有,请坚持wxString

    【讨论】:

    • 您是否将std::string string01; 添加到class inheritedgui 的正文中?如果是这样,您不需要在m_button1OnButtonClick 中再次声明它。只需使用string01 = m_textCtrl1-&gt;GetValue().ToStdString();
    • 进行了一些更改 (pastebin.com/ejqyMnG5) 现在代码构建良好(added std::string string01;class inheritedgui 的主体),但是单击“m_button2”后仍然没有任何反应。如果我将std::string 更改为wxString,则会收到以下错误:` ^ /home/charlesdward/Documents/Workspace/FP_Clinic/inheritedgui.cpp:22:15: error: call to constructor of 'wxString' is ambiguous wxMessageBox(wxString(string01.c_str(), wxConvUTF8), wxT("这是标题"), wxICON_INFORMATION);`
    【解决方案2】:

    您不需要像我在上一个问题中所说的那样,将从文本控件收到的值转换为 std::string。

    您还需要将此 wxString 声明为该类的成员。

    为了修复编译错误:

    wxMessageBox( string01, wxT("This is the title"), wxICON_INFORMATION );
    

    另外,最好使用Bind() 而不是Connect()

    Bind( wxEVT_CLOSE_WINDOW, &inheritedGui::OnCloseDialog, this );
    Bind( wxEVT_COMMAND_BUTTON_CLICKED, &inheritedGui::m_button1OnButtonClick, this );
    

    在析构函数中,您不必UnBind() 事件 - 它会自动发生。

    最后——你不必声明函数virtual——没有它也可以工作。实际上 wxWidgets 中的事件处理程序很少应该是虚拟的。

    谢谢。

    【讨论】:

    • 代码是由 wxFormBuilder 自动生成的,如果我将 Connect 更改为 Bind,这些更改将在稍后再次使用 wxFormBuilder 时恢复......但是感谢您的指示,我会记住这一点我学会了手工编写图形用户界面
    • 我对您提到的 wxString 和 wxMessageBox 进行了更正,对inheritedgui.cpp:pastebin.com/4HCFqYVM 其余代码(gui.h、gui.cpp、inheritedgui.h)保持不变第一篇文章。现在我没有收到编译错误,但是当我按下“m_button2”时没有任何反应。希望你能帮我解决这个问题,我问是因为我被卡住了。最好的问候,感谢您的帮助
    • 我编辑了主帖,所以你不需要输入 pastebin 链接
    • @etcCoder,我编辑了我的答案。另外,我没有看到您的 OP 的编辑,因为我仍然看到 Connect() 在那里打电话。另外,只是好奇——你为什么要从MainDialogBase 继承一个类?为什么不把所有东西都放在一个主类中?
    • 我尝试将“connect”更改为“bind”,但出现构建错误。这是编辑后的代码和构建错误:pastebin.com/A7WnPppt。将“连接”更改为“绑定”是否会显示 wxMessageBox?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-04-15
    • 1970-01-01
    • 2021-04-25
    • 1970-01-01
    • 1970-01-01
    • 2019-06-18
    • 2020-11-27
    相关资源
    最近更新 更多