Connect() bug, Isn't activation function

If you are using the main C++ distribution of wxWidgets, Feel free to ask any question related to wxWidgets development here. This means questions regarding to C++ and wxWidgets, not compile problems.
Post Reply
E-man96
In need of some credit
In need of some credit
Posts: 8
Joined: Sun Mar 07, 2010 2:36 am
Location: California

Connect() bug, Isn't activation function

Post by E-man96 »

For some reason the line
Connect(createid, wxEVT_COMMAND_BUTTON_CLICKED, wxCommandEventHandler(MainFrame::CreatePart),NULL,create);
doesn't activate the function CreatePart(). I've tried it with the last argument as "this", and without the last two arguments at all. I couldn't think of anything to research for because I don't know what's wrong. Can someone tell me what I should've looked for and why the program doesn't work. Anything is helpful.

Code: Select all

#include"wx/wxprec.h"

#ifndef WX_PRECOMP
    #include"wx/wx.h"
#endif

#include"wx/wxprec.h"
#include<iostream>

#ifndef WX_PRECOMP
    #include"wx/wx.h"
#endif

#define playid 404
#define createid 666

class CreateFrame : public wxFrame
  {
  };

class MainFrame : public wxFrame
  {
    public:
      MainFrame();
      void CreatePart(wxCommandEvent& event);
  };

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

MainFrame::MainFrame()
    :wxFrame(NULL, wxID_ANY, "Word Timer", wxDefaultPosition, wxDefaultSize)
  {
    this->SetSizeHints(wxSize(200,330), wxSize(200,330));
    wxBoxSizer *mainpanelholder=new wxBoxSizer(wxVERTICAL);
    wxPanel *mainpanel=new wxPanel(this, wxID_ANY, wxDefaultPosition, wxDefaultSize);
    wxGridSizer *buttonbox=new wxGridSizer(2,1,0,0);
    wxButton *create, *play;
    create=new wxButton(mainpanel, createid, wxT("Create a timing"));
    play  =new wxButton(mainpanel, playid, wxT("Play a timing")  );
    buttonbox->Add(play,1,wxEXPAND|wxALL,5);
    buttonbox->Add(create,1,wxEXPAND|wxALL,5);
    mainpanel->SetSizer(buttonbox);
    mainpanelholder->Add(mainpanel,1,wxALL|wxEXPAND, 25);
    SetSizer(mainpanelholder);
    //this->Centre();
    //Connect(ctoqbuttonclick_ID, wxEVT_COMMAND_BUTTON_CLICKED,wxCommandEventHandler(MyFrame::Onctoqclick),NULL,this);
    Connect(createid, wxEVT_COMMAND_BUTTON_CLICKED, wxCommandEventHandler(MainFrame::CreatePart),NULL,create);
  }

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

void MainFrame::CreatePart(wxCommandEvent& event)
  {
    std::cout<<"oeirtfoienasrtoiensrta";
    //wxFileDialog *openfile=new wxFileDialog(this,wxT("Select the file to read"),wxT(""),wxT(""),wxOPEN|wxFILE_MUST_EXIST,wxDefaultPosition);
  }

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

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

IMPLEMENT_APP(Lyrics)

bool Lyrics::OnInit()
  {
    MainFrame *blah=new MainFrame();
    blah->Show(true);
  }
penisfrog
jfouche
Super wx Problem Solver
Super wx Problem Solver
Posts: 442
Joined: Tue May 06, 2008 4:52 pm
Location: France

Post by jfouche »

Hi

What does the line

Code: Select all

Connect(createid, wxEVT_COMMAND_BUTTON_CLICKED, wxCommandEventHandler(MainFrame::CreatePart),NULL,create);
mean ?

You connect the event BUTTON_CLICKED of the createid control ID on the MainFrame::CreatePart method, without data, on the create control handler. That's wrong because the method is not part of the create button, but it's a MainFrame method. You must use a MainFrame instance of this class as the handler.

For a better reading, I prefer using the following :

Code: Select all

create->Connect(wxEVT_COMMAND_BUTTON_CLICKED, wxCommandEventHandler(MainFrame::CreatePart),NULL,this);
I explicitly connect the BUTTON_CLICKED event of the create control.
Jérémie
DavidHart
Site Admin
Site Admin
Posts: 4252
Joined: Thu Jan 12, 2006 6:23 pm
Location: IoW, UK

Post by DavidHart »

Hi,

First,
Connect(createid, wxEVT_COMMAND_BUTTON_CLICKED, wxCommandEventHandler(MainFrame::CreatePart), NULL, create);
is wrong: the last parameter, the eventSink, should be a pointer to an instance of the handler function; in this case MainFrame.

The best way to do what you want is:

Code: Select all

create=new wxButton(mainpanel, wxID_ANY, wxT("Create a timing"));
create->Connect(create.GetId(), wxEVT_COMMAND_BUTTON_CLICKED, wxCommandEventHandler(MainFrame::CreatePart), NULL, this);
You're connecting the button (thus the initial create->) to the function MainFrame::CreatePart in the MainFrame instance 'this'. You don't really care about the ID of the event, since there's only going to be one button's event arriving there, so you could replace the create->GetId() with wxID_ANY (or with nothing, as there's an overload that does this). However using a specified ID in both places should also work.

However, wxCommandEvents like button-clicks, if unhandled, propagate to parent controls; in this case first the panel, then the frame. So the following should also have worked:
Connect(wxEVT_COMMAND_BUTTON_CLICKED, wxCommandEventHandler(MainFrame::CreatePart), NULL, this);

Are you sure that MainFrame::CreatePart wasn't called? Did you put a breakpoint inside it, or uncomment the wxFileDialog?

Regards,

David
artdias90
In need of some credit
In need of some credit
Posts: 6
Joined: Sat Feb 05, 2011 4:13 am

Post by artdias90 »

I have a question:

instead of pushing a button, I want my clock to start as soon as the aplication starts. How should me my "connect" ?
User avatar
evstevemd
Part Of The Furniture
Part Of The Furniture
Posts: 2408
Joined: Wed Jan 28, 2009 11:57 am
Location: United Republic of Tanzania

Post by evstevemd »

artdias90 wrote:I have a question:

instead of pushing a button, I want my clock to start as soon as the aplication starts. How should me my "connect" ?
Start the timer then in the constructor
Chief Justice: We have trouble dear citizens!
Citizens: What it is his honor?
Chief Justice:Our president is an atheist, who will he swear to?
Post Reply