SetLabel() Problem



  • Hallo Leute,

    also ich hoffe ihr könnt mir helfen!

    Also ich habe aus diesem Forum herausgelesen, wie man bei einem wxStaticText ein das Label ändern kann, leider funktioniert es nicht wirklich.

    Also habe in einem Panel ein wxStaticText. Ich will das das Label bei diesem wxStaticText ändert, wenn ich einen wxButton drücke.

    so habe ich es programmiert:

    #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
    #pragma implementation "haupt.h"
    #endif
    
    // For compilers that support precompilation, includes "wx/wx.h".
    #include "wx/wxprec.h"
    
    #ifdef __BORLANDC__
    #pragma hdrstop
    #endif
    
    #ifndef WX_PRECOMP
    #include "wx/wx.h"
    #endif
    
    ////@begin includes
    #include "haupt.h"
    ////@end includes
    
    #include "haupt.h"
    
    ////@begin XPM images
    ////@end XPM images
    
    /*!
     * haupt type definition
     */
    
    IMPLEMENT_CLASS( haupt, wxFrame )
    
    /*!
     * haupt event table definition
     */
    
    BEGIN_EVENT_TABLE( haupt, wxFrame )
    
    ////@begin haupt event table entries
        EVT_BUTTON( ID_BUTTON1, haupt::OnButton1Click )
    
    ////@end haupt event table entries
    
    END_EVENT_TABLE()
    
    /*!
     * haupt constructors
     */
    
    haupt::haupt()
    {
        Init();
    }
    
    haupt::haupt( wxWindow* parent, wxWindowID id, const wxString& caption, const wxPoint& pos, const wxSize& size, long style )
    {
        Init();
        Create( parent, id, caption, pos, size, style );
    }
    
    /*!
     * haupt creator
     */
    
    bool haupt::Create( wxWindow* parent, wxWindowID id, const wxString& caption, const wxPoint& pos, const wxSize& size, long style )
    {
    ////@begin haupt creation
        wxFrame::Create( parent, id, caption, pos, size, style );
    
        CreateControls();
        Centre();
    ////@end haupt creation
        return true;
    }
    
    /*!
     * haupt destructor
     */
    
    haupt::~haupt()
    {
    ////@begin haupt destruction
    ////@end haupt destruction
    }
    
    /*!
     * Member initialisation 
     */
    
    void haupt::Init()
    {
    ////@begin haupt member initialisation
    ////@end haupt member initialisation
    }
    
    /*!
     * Control creation for haupt
     */
    
    void haupt::CreateControls()
    {    
    ////@begin haupt content construction
        haupt* itemFrame1 = this;
    
        wxPanel* itemPanel2 = new wxPanel( itemFrame1, ID_PANEL1, wxDefaultPosition, wxDefaultSize, wxSUNKEN_BORDER|wxTAB_TRAVERSAL );
    
        wxBoxSizer* itemBoxSizer3 = new wxBoxSizer(wxVERTICAL);
        itemPanel2->SetSizer(itemBoxSizer3);
    
        itemBoxSizer3->Add(5, 100, 0, wxALIGN_CENTER_HORIZONTAL|wxALL, 5);
    
        wxBoxSizer* itemBoxSizer5 = new wxBoxSizer(wxHORIZONTAL);
        itemBoxSizer3->Add(itemBoxSizer5, 0, wxALIGN_CENTER_HORIZONTAL|wxALL, 5);
    
        itemBoxSizer5->Add(5, 5, 0, wxALIGN_CENTER_VERTICAL|wxALL, 5);
    
        wxStaticText* itemStaticText7 = new wxStaticText( itemPanel2, wxID_STATIC, wxT("Static text"), wxDefaultPosition, wxDefaultSize, 0 );
        itemBoxSizer5->Add(itemStaticText7, 0, wxALIGN_CENTER_VERTICAL|wxALL, 5);
    
        wxButton* itemButton8 = new wxButton( itemPanel2, ID_BUTTON1, _("Drucken"), wxDefaultPosition, wxDefaultSize, 0 );
        itemBoxSizer3->Add(itemButton8, 0, wxALIGN_CENTER_HORIZONTAL|wxALL, 5);
    
    ////@end haupt content construction
    }
    
    /*!
     * Should we show tooltips?
     */
    
    bool haupt::ShowToolTips()
    {
        return true;
    }
    
    /*!
     * Get bitmap resources
     */
    
    wxBitmap haupt::GetBitmapResource( const wxString& name )
    {
        // Bitmap retrieval
    ////@begin haupt bitmap retrieval
        wxUnusedVar(name);
        return wxNullBitmap;
    ////@end haupt bitmap retrieval
    }
    
    /*!
     * Get icon resources
     */
    
    wxIcon haupt::GetIconResource( const wxString& name )
    {
        // Icon retrieval
    ////@begin haupt icon retrieval
        wxUnusedVar(name);
        return wxNullIcon;
    ////@end haupt icon retrieval
    }
    
    /*!
     * wxEVT_COMMAND_BUTTON_CLICKED event handler for ID_BUTTON1
     */
    
    void haupt::OnButton1Click( wxCommandEvent& event )
    {
    ////@begin wxEVT_COMMAND_BUTTON_CLICKED event handler for ID_BUTTON1 in haupt.
        itemStaticText7->SetLabel(wxT("BaluBlub"));
        haupt* window = new haupt(this, ID_HAUPT, _("haupt"));
        window->Show(true);
    ////@end wxEVT_COMMAND_BUTTON_CLICKED event handler for ID_BUTTON1 in haupt. 
    }
    

    Also in in .h Datei habe ich wxStaticText itemStaticText7;* deklariert.

    Das Programm läuft ohne Probleme, bloss beim Drücken des Buttons stürzt das Programm ab und ich bekomme folgendes Fehler heraus.

    Segmentation fault (core dumped)
    ***Exited with status: 139***

    Danke im voraus


  • Mod

    2 Dinge fallen mir auf:
    1. Du erstellst du eine Lokale wxStaticText Variable im Konstruktor
    2. Wieso erstellst du eine neue Fensterinstanz im Buttonhandler?

    phlox



  • Ich habe das aus einem Beispiel kopiert, da ich ein Neuling in GUI Programmierung bin.
    Kannst du mir dass vielleicht genauer erklären und evt. das mit SetLabel() helfen?

    Gruss und Dank


  • Mod

    Du überdeckst deine Membervariable im Konstruktor mit einer lokalen Variablen.
    Das wxStaticText* muss aus dem Konstruktor weg.



  • ok, dass habe ich verstanden.
    Aber dann bekomme ich bei compelieren folgendes Fehler.

    ‘itemStaticText7’ was not declared in this scope
    

    Gruss und Dank


  • Mod

    Hm, ich dachte das wär ne Membervariable.
    Das muss sie schliesslich sein, wenn du von anderen Memberfunktionen daraufzugreifen willst.
    Und du solltest dir mal die Artikel im Magazin zu wxWidgets durchlesen.



  • danke für die Antworten.

    leider hat es mir nicht wirklich wa sgebracht.



  • swunder schrieb:

    leider hat es mir nicht wirklich wa sgebracht.

    Selber Schuld.

    1. Tutorial lesen
    2. Membervariable anlegen oder ID bzw Name (für FindWindowBy* ) vergeben
    3. Sinnvolle Bezeichner wählen

Anmelden zum Antworten