Newbie trying to get to grips with events.

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
philjynx
Earned some good credits
Earned some good credits
Posts: 137
Joined: Tue Mar 06, 2018 6:00 pm

Newbie trying to get to grips with events.

Post by philjynx »

My background in Delphi and Arduino is not helping here!
At its most basic, I am trying to get a button click on my main wxWidget form to launch another form.
I can do that if I put the following in the derived class from wxFormBuilder:
I haven't even begun to look at how my main app is going to know about values changed in child forms.

Code: Select all

.....
void stestMyFrame1::OnOtherForm( wxCommandEvent& event )
{
	stestMyFrame2* subFrame = new stestMyFrame2(NULL);
	subFrame->show(true);
}
My infant project compiles, runs and does as it should, displays the main form and when I click on the button for the subform, that gets displayed.

There are two problems with this.
1 It depends on #include "stestMyFrame2.h" which I put at the top of this generated file.
When you "generate inherited class" (in wxFormBuilder), the #include gets wiped out.

My other code (in the block above) is left intact, but is unusable because the dependency has been broken.

2 When it is in a functioning state, you can click and create an infinite number of instances of subform.

So, clearly my approach is unworkable.

There appears not to be a ShowModal available, that would enforce only displaying one instance of a form.
I've tried to get to grips with the part of the manual that deals with event handling.
Events and Event Handling.

One of the many hurdles that I've fallen on is this:
"Next the event table must be defined and, as with any definition, it must be placed in an implementation file. The event table tells wxWidgets how to map events to member functions and in our example it could look like this:"
particularly "...it must be placed in an implementation file."
What implementation file?

I'm not looking for a course in C++ here, but honestly, I'm finding this extremely hard to follow - is there an example of all those fragments stitched together into a whole that can be perused and digested?

I imagine that as I am trying to use inherited classes from wxFormBuilder that that is obfuscating this even more for me.

I do a lot of coding for teensy - with a great deal of event handling - it's really easy, as was Delphi.
So, I understand the concepts, but I'm not following how to do this with wxWidgets.
Help!
Last edited by philjynx on Tue Mar 13, 2018 3:10 pm, edited 2 times in total.
ONEEYEMAN
Part Of The Furniture
Part Of The Furniture
Posts: 7458
Joined: Sat Apr 16, 2005 7:22 am
Location: USA, Ukraine

Re: Newbie trying to get to grips with events.

Post by ONEEYEMAN »

Hi,
For your number 1 issue - wxFB as other RAD tools generates the code has parts of "{{{ do not change - autogenerated code }}}". If you put your include outside of such block it will not be wiped.

You can also use an OOP paradigm here and leave the generated GUI file along. Just derive your own class from it and put all you event handlers in it. Then if later on you will need to redo some of the designs it will be easy - your own code will be intact.

For your number 2 issue - you should use wxDialog as a base class and not wxFrame. Then you will be able to use "ShowModal()" call on that object.
Please check the documentation for ShowModal() and see which class it belongs to.

Thank you.
philjynx
Earned some good credits
Earned some good credits
Posts: 137
Joined: Tue Mar 06, 2018 6:00 pm

Re: Newbie trying to get to grips with events.

Post by philjynx »

Thanks, both of those suggestions make perfect sense.
That just leaves the unfathomable event handling.


The do not edit caveat you mentioned doesn't actually exist in the derived class from FB
Here's mine any edits I do within the generated functions are left intact, edit anywhere else and it vanishes.
But, your suggestion of deriving from this derived file still stands as good.
#include "stestMyFrame1.h"

stestMyFrame1::stestMyFrame1( wxWindow* parent )
:
MyFrame1( parent )
{


}

void stestMyFrame1::OnQuit( wxCommandEvent& event )
{
// TODO: Implement OnQuit
}

void stestMyFrame1::OnOtherForm( wxCommandEvent& event )
{
// TODO: Implement OnOtherForm
}
ONEEYEMAN
Part Of The Furniture
Part Of The Furniture
Posts: 7458
Joined: Sat Apr 16, 2005 7:22 am
Location: USA, Ukraine

Re: Newbie trying to get to grips with events.

Post by ONEEYEMAN »

Hi,
Are you referring to this:
One of the many hurdles that I've fallen on is this:
"Next the event table must be defined and, as with any definition, it must be placed in an implementation file. The event table tells wxWidgets how to map events to member functions and in our example it could look like this:"
particularly "...it must be placed in an implementation file."
What implementation file?
?

The C++ source contains 2 files: .h and .cpp. .h file is called header file or interface file - this is where you put this:

Code: Select all

#ifndef SOMEMACRO
#define SOMEMACRO

class Foo
{
public:
    Foo();
    ~Foo();
private:
// all members goes here
};

#endif
The .cpp file is called an implementation file and that's where the source code for all functions goes.

Now the .cpp includes .h file(s) in order to know the definition of all different classes that this particular .cpp file (AKA translation unit) uses.

The C++ program structure is different from Delphi in this regards - IIRC, Delphi translation unit contains only one file that is generated from the GUI form. C++ code contains 2.

Thank you.

P.S.: Event table are the old way of handling events. Take a look at event binding in the documentation and in the event sample. Event table is still works and will work in the future, but it is better to get a grip on the dynamic event handling as well. Read the docs, check the sample and you will be good to go.
philjynx
Earned some good credits
Earned some good credits
Posts: 137
Joined: Tue Mar 06, 2018 6:00 pm

Re: Newbie trying to get to grips with events.

Post by philjynx »

ONEEYEMAN wrote:Hi,
Are you referring to this:

The .cpp file is called an implementation file and that's where the source code for all functions goes.
.....

P.S.: Event table are the old way of handling events. Take a look at event binding in the documentation and in the event sample. Event table is still works and will work in the future, but it is better to get a grip on the dynamic event handling as well. Read the docs, check the sample and you will be good to go.
I am avoiding use of the event table.

From the manual -

Code: Select all

void MyFrameHandler::OnFrameExit( wxCommandEvent & )
{
    // Do something useful.
}
MyFrameHandler myFrameHandler;
MyFrame::MyFrame()
{
      Bind( wxEVT_COMMAND_MENU_SELECTED, &MyFrameHandler::OnFrameExit,
              &myFrameHandler, wxID_EXIT );
}
I was eventually able to fathom that out, whoever wrote that has an obtuse sense of humour, using MyFrameHandler and myFrameHandler like that, is that really meant to make the situation clear to anyone trying to learn?

Anyhow, unlike the manual, you have been really helpful and I am now able to write code that works. It probably leaks, but I can get to that too, in time.
Thank you very much for your help.
ONEEYEMAN
Part Of The Furniture
Part Of The Furniture
Posts: 7458
Joined: Sat Apr 16, 2005 7:22 am
Location: USA, Ukraine

Re: Newbie trying to get to grips with events.

Post by ONEEYEMAN »

Hi,
This is definitely a weird naming convention. ;-)
And you are welcome. That's why we are here - to help people.

If you have any more questions - don't hesitate. Keep in mind though that the best way to learn is to check different samples provided with wx.
And if you have a problem with anything - they are you first stop in the investigation process.

I am about to say something "politically incorrect". Apologies if I offend anyone. But I'm myself an ESL person and so I know what I'm talking about.

Also keep in mind - documentation is written by many different people. Some of them are ESL (English as Second Language) people. So it is possible that reading the documentation may not be very clear. And so - the best way to understand something is to look at the samples and check the sources.

Thank you.
User avatar
doublemax
Moderator
Moderator
Posts: 19114
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: Newbie trying to get to grips with events.

Post by doublemax »

FWIW, static event tables are not outdated, i personally use them whenever possible. They're less to type, easier to read and easier to extend. They just can't be used for every purpose.
Use the source, Luke!
Post Reply