Working with event between classe Topic is solved

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
marcelovvm
Experienced Solver
Experienced Solver
Posts: 58
Joined: Tue Mar 28, 2006 7:18 am
Location: Rio de Janeiro - Brasil
Contact:

Working with event between classe

Post by marcelovvm »

Dear friends,

I'm developping an app with db access throught the SQLAPI classe, so my class to access data base is no wx derived. I create one class called MyDB it is derived from SAConnection (the classe to create a connection object). My app has a frame with icons to access db, ie, connect and disconnect.

Until now my db access is working because a put at MyFrame class (derived from wxFrame) the connect method, and I create a instance of MyDB called db_con. So my problem is, I want to put all my method to access data base (connect, disconnect, query...) in the MyDB class.

I know with this is possible, but I need to use event, and.. so .. do dont know anything about how to get and send event between class. I only work with event in the same class. Someone could help me with this?

my_frame.h

Code: Select all

#ifndef __myframe_H
#define __myframe_H

class MyFrame: public wxFrame
{
public: 
	...//other methods

	void OnConnect( wxCommandEvent& event );

	void OnDisconnect( wxCommandEvent& event );

	void OnQuery( wxCommandEvent& event );

private:

	...//other methods

	DECLARE_EVENT_TABLE()
	
};

#endif
myframe.cpp

Code: Select all

...
#include "myframe.h"
#include "mydb.h"
...

MyDB *db_con = new MyDB;

BEGIN_EVENT_TABLE( MyFrame, wxFrame ) 
                ...
	EVT_MENU( ID_Connect , MyFrame::OnConnect)
	EVT_MENU( ID_Disconnect , MyFrame::OnDisconnect )
	EVT_MENU( ID_Query , MyFrame::OnQuery )
                ...
END_EVENT_TABLE()

void MyFrame::OnConnect(wxCommandEvent& WXUNUSED(event)) 
{
     ...
     db_con->setClient(SA_SQLServer_Client);
     db_con->Connect(... the necessery parameters...);
     ...
}
mydb.h

Code: Select all


#ifndef __mydb_H
#define __mydb_H

class MyDB: public SAConnection
{
public:

	bool QueryDB( MyDB *con , MyFrame *frame );
	
private:

};

#endif
mydb.cpp -> query something at db.

[]s.
Marcelo V. V. Magalhães, CSM
E+D Consultores Associados
Análise, Projeto e Desenolvimento de sistemas
www.emaisd.com.br
Rio de Janeiro - R.J.
Cel.: +55 (21) 96140028
Cursor
Earned some good credits
Earned some good credits
Posts: 120
Joined: Sun Aug 29, 2004 3:09 pm
Location: Grenoble, France
Contact:

Post by Cursor »

You can manipulate and send events directly.
You just have to create an event object and send it [1] to your target object.

Like that witch simulate a clicked button event:

Code: Select all

wxCommandEvent evt(wxEVT_COMMAND_BUTTON_CLICKED, WIDGET_ID);
pWidget->ProcessEvent(evt);
Note that all wxEvtHandler derived class (like all widgets) can process events.

[1] : http://www.wxwidgets.org/manuals/2.6.3/ ... ocessevent
What is little and green, witch go up and down ??
Yoda playing with the force.
marcelovvm
Experienced Solver
Experienced Solver
Posts: 58
Joined: Tue Mar 28, 2006 7:18 am
Location: Rio de Janeiro - Brasil
Contact:

Post by marcelovvm »

Thanks Cursor, but could you explain with more details how can I do this? Change me code (first message) and include the modification.

[]s.
Marcelo V. V. Magalhães, CSM
E+D Consultores Associados
Análise, Projeto e Desenolvimento de sistemas
www.emaisd.com.br
Rio de Janeiro - R.J.
Cel.: +55 (21) 96140028
Cursor
Earned some good credits
Earned some good credits
Posts: 120
Joined: Sun Aug 29, 2004 3:09 pm
Location: Grenoble, France
Contact:

Post by Cursor »

In fact, I'm not sure I undertand you.

Do you want to pass (transmit) all events all events to your MyDB object in order to handle them by MyDB object ?
What is little and green, witch go up and down ??
Yoda playing with the force.
marcelovvm
Experienced Solver
Experienced Solver
Posts: 58
Joined: Tue Mar 28, 2006 7:18 am
Location: Rio de Janeiro - Brasil
Contact:

Post by marcelovvm »

I can do this. When I clickon the connect icon at toolbar one event is create end send to MyDB class. This event will "tell" to MyDB class to execute OnConnect member.

But, attention, when OnConnect member is execute the connect variable (i.e. db_con) is create, ant this variable is necessery for all other member of MyDB class.

Do you know how to do tihs?

[]s.
Marcelo V. V. Magalhães, CSM
E+D Consultores Associados
Análise, Projeto e Desenolvimento de sistemas
www.emaisd.com.br
Rio de Janeiro - R.J.
Cel.: +55 (21) 96140028
Cursor
Earned some good credits
Earned some good credits
Posts: 120
Joined: Sun Aug 29, 2004 3:09 pm
Location: Grenoble, France
Contact:

Post by Cursor »

Ok so if you want to pass all events to your MyDB object, you must derive it from wxEvtHandler [1]. IMHO, it is not a problem to derive MyDB from SAConnection an wxEvtHandler.

So, as all wxEvtHandler-based class, you can declare and implement an event table via DECLARE_EVENT_TABLE() and BEGIN/END_EVENT_TABLE() [2] and intercept all event you want.

In order to pass events to your MyDB event handler, you can derive ProcessEvent [3] function to pass events to your MyDB event handler or (INHO, the best choice) directly plug the MyDB event handler to the main frame event handler with wxWindow::PushEventHandler [4].


[1] : http://www.wxwidgets.org/manuals/2.6.3/ ... evthandler
[2] : http://www.wxwidgets.org/manuals/2.6.3/ ... ngoverview
[3] : http://www.wxwidgets.org/manuals/2.6.3/ ... ocessevent
[4] : http://www.wxwidgets.org/manuals/2.6.3/ ... enthandler
What is little and green, witch go up and down ??
Yoda playing with the force.
Post Reply