8 Replies - 1729 Views - Last Post: 27 January 2010 - 01:37 PM

#1 scared0o0rabbit  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 18
  • Joined: 20-September 09

A few questions about using wxWidgets

Posted 26 January 2010 - 11:10 AM

I'm fairly new to using wxWidgets, programming GUI applications, and event driven programming in general. I had a few questions that I am hoping are fairly easy and uncomplicated (I can hope) to answer.

What I've been using so far to do the little bit of work I've done is code blocks and wxSmith. I figure I probably sould learn how to do some of this by hand so I have some idea of what I'm actually seeing when I look at my code. So my first question is, are there any good tutorials for a beginner for this purpose?

Second, so far since I haven't really known how to pass data to the functions (events?) I've mostly used global variables when I've written a wxwidgets application. In plain C++ you'd declare and then define the function and inside the parenthesis you'd put the variables you'd like to pass to the function. In wxWidgets it creates things like this:
void Castle_Age_LandDialog::OnButton1Click(wxCommandEvent& event)
{
}

and so far I can include code inside that block and it will act like a function when the event happens, but I'd love to be able to pass variables in when that happens instead of just using global variables. Is this possible? If so, how do I do it? Do I need to modify a declaration somewhere too?

Third, it seems that when I create a normal c++ style function I can't interact with the wxWidgets stuff at all. That is to say, something like this won't work:
bool fullRecalculate(){
	//These two variables will be used for conversions.
	wxString tempWXString;
	double temp;
	//We're not going to do any user error input checking in this version
	//maybe in the next version...
	//Gets the amount of gold you have and stores it
	tempWXString = TextCtrl9->GetValue();
	return true;
}

(assuming that TextCtrl9 is something that actually exists). This code works fine (minus the return true) in the event from above, but doesn't work in my function. Is there something I have to do to interact with the wxwidgets stuff from a regular function or is there some other kind of function I can create that isn't a regular c++ function that the events can call rather than having to copy and paste hundreds of lines of code in several places in my source?

Lastly, is there a way to call an event? Let's say I have a button that when pushed performs a bunch of math on a bunch of boxes that have been filled in. Is there a way I can tell other places in the code to act like the button was pushed?

Hopefully at least some of these questions are easily answered, because after taking some c++ classes and then jumping into the deep end, so to speak, where I'm going from simple CLI programs to GUI programs I feel like I'm ending up with code that's a lot sloppier and things just don't seem right (my application works, but it seems, to me, like there are many things that should work, but don't).

Thanks in advance for any guidance anyone can offer.

Is This A Good Question/Topic? 0
  • +

Replies To: A few questions about using wxWidgets

#2 r.stiltskin  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1833
  • View blog
  • Posts: 4,927
  • Joined: 27-December 05

Re: A few questions about using wxWidgets

Posted 26 January 2010 - 01:06 PM

View Postscared0o0rabbit, on 26 Jan, 2010 - 01:10 PM, said:

So my first question is, are there any good tutorials for a beginner for this purpose?

Look at wxWiki

Quote

I'd love to be able to pass variables in when that happens instead of just using global variables. Is this possible? If so, how do I do it?

It's difficult to answer that without knowing exactly what you want to accomplish. For example, you can use a wxTextCtrl in which the user can enter data that can be read by your function, wxButton that the user can use to provide boolean (true/false, on/off ...) input, wxListbox to present the user with a list of choices, etc. If this is not what you had in mind, give an example of the sort of variables you want to pass in (and where are they coming from)?

Quote

... (assuming that TextCtrl9 is something that actually exists). This code works fine (minus the return true) in the event from above, but doesn't work in my function. Is there something I have to do to interact with the wxwidgets stuff from a regular function or is there some other kind of function I can create that isn't a regular c++ function that the events can call rather than having to copy and paste hundreds of lines of code in several places in my source?

What is it that you claim doesn't work? And what do you mean about pasting "hundreds of lines of code in several places..."? An ordinary c++ function works fine, as long as you provide something (e.g. define an event) that will call the function. For example, you can take any command-line program that prompts the user for input and replace those prompts with wxTextCtrls and wxButtons that call the appropriate functions, including passing any required parameters to them. If I'm missing your point, please elaborate.

Quote

Lastly, is there a way to call an event? Let's say I have a button that when pushed performs a bunch of math on a bunch of boxes that have been filled in. Is there a way I can tell other places in the code to act like the button was pushed?

I think you already have the answer to that.
void Castle_Age_LandDialog::OnButton1Click(wxCommandEvent& event)
{
// here, you can read the input that the user provided in the boxes and call the functions that you want to execute when the button is pushed
}

Was This Post Helpful? 0
  • +
  • -

#3 scared0o0rabbit  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 18
  • Joined: 20-September 09

Re: A few questions about using wxWidgets

Posted 26 January 2010 - 04:00 PM

In my program I do a lot of work on the input the user has given. While it's entirely possible for me to read that input and perform the calculation, it seems like there has to be a better way.

I guess I should try and explain what I'm trying to do and maybe you can tell me if I'm just SOL or if there actually is a shortcut.

In a nutshell I have say 10 boxes that input gets put into. Then I take that input and I perform a bunch of calculations based on each and all of the numbers. I then display the results of all the calculations in something like 30 different labels on the display of the program. I understand that when the event of an item being updated is detected I can have it store that into a variable that I could then pass to a normal c++ function and have it do all the math, but then unless I go back into an event I can't update all the labels that need to be updated.

Is there a way that I could create a function that I can call for each event that I want it to be called for that will update all my labels and perform all my calculations so that I don't need to have 30+ lines of code that is identical for each event?

As far as passing the variables goes, what happens if I have some variable that doesn't come from user input that I want to be able to have an event use. Is there an easy way to give the event access to that variable without making it a global variable?
Was This Post Helpful? 0
  • +
  • -

#4 r.stiltskin  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1833
  • View blog
  • Posts: 4,927
  • Joined: 27-December 05

Re: A few questions about using wxWidgets

Posted 26 January 2010 - 04:11 PM

Exactly how you do it depends on the sequence of when variables get updated, when calculations are performed (e.g. when each individual text box is updated, or after all or some subset of them is updated, etc.), when labels are updated (again, separately after each calculation, or as a group after multiple calculations are completed) so I can't give you a blanket answer that covers all cases. But, for example, you can let your user change entries in one or more boxes and then press a button, and that button fires an event that calls a function that reads the new entries, updates the variables, calls the calculation function(s), and then updates whatever labels are affected.
Was This Post Helpful? 0
  • +
  • -

#5 scared0o0rabbit  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 18
  • Joined: 20-September 09

Re: A few questions about using wxWidgets

Posted 26 January 2010 - 09:56 PM

View Postr.stiltskin, on 26 Jan, 2010 - 03:11 PM, said:

Exactly how you do it depends on the sequence of when variables get updated, when calculations are performed (e.g. when each individual text box is updated, or after all or some subset of them is updated, etc.), when labels are updated (again, separately after each calculation, or as a group after multiple calculations are completed) so I can't give you a blanket answer that covers all cases. But, for example, you can let your user change entries in one or more boxes and then press a button, and that button fires an event that calls a function that reads the new entries, updates the variables, calls the calculation function(s), and then updates whatever labels are affected.


So there's no way to have a function update the labels? Is there some other way that I can have a block of code that will update the labels that can be called from every event I want it to, or am I just stuck updating my labels in every event that would need to update the labels?
Was This Post Helpful? 0
  • +
  • -

#6 r.stiltskin  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1833
  • View blog
  • Posts: 4,927
  • Joined: 27-December 05

Re: A few questions about using wxWidgets

Posted 26 January 2010 - 10:10 PM

View Postscared0o0rabbit, on 26 Jan, 2010 - 11:56 PM, said:

So there's no way to have a function update the labels? Is there some other way that I can have a block of code that will update the labels that can be called from every event I want it to ...

Why not? What's stopping you from writing a single function that updates the labels, and calling that function from every event handler?
Was This Post Helpful? 0
  • +
  • -

#7 scared0o0rabbit  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 18
  • Joined: 20-September 09

Re: A few questions about using wxWidgets

Posted 27 January 2010 - 10:00 AM

Well that's just it, a normal c++ function seems to not be able to access the labels and input boxes. When I try and compile with a function like that defined I get about 50 compile errors that say things like:

‘TextCtrl1’ was not declared in this scope

Is there some way I can use the scope resolution operator to access them? When I try and do something like this:

tempWXString = mainWindows::TextCtrl9->GetValue();


I get a whole bunch of errors that say that the TextCtrl9 member is private, and that it is an illegal use of a non-static data member. I get the same issue when trying to actually update the label with a line of code like this:

mainWindow::StaticText11->SetLabel(tempWXString);


If I go and make it public though, it still isn't enough to make it work as I still get the message about an illegal ue of a non-static data member. Is there some other syntax for getting at the items on the GUI?
Was This Post Helpful? 0
  • +
  • -

#8 r.stiltskin  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1833
  • View blog
  • Posts: 4,927
  • Joined: 27-December 05

Re: A few questions about using wxWidgets

Posted 27 January 2010 - 11:36 AM

Of course you can't use the scope resolution operator to access the mainWindow members unless you declare them to be static (and I don't know if wxWidgets will let you do that).

But I assume your mainWindow class is in [something]Main.cc, and you have a corresponding [something]Main.h. If you #include [something]Main.h in the file that contains the update function, that should solve the "was not declared" errors. To change the labels, leave the widgets private but add a public method to your mainWindow class to set their values. Your public method can call the SetLabel method of the wxStaticText.

Have you looked at the wxWidgets API : "wxWidgets 2.8.7: A portable C++ and Python GUI toolkit", Julian Smart, Robert Roebling, Vadim Zeitlin, Robin Dunn, et al
November, 2007. It should be part of the package that you installed, and you can probably find it online as well.

This post has been edited by r.stiltskin: 27 January 2010 - 11:37 AM

Was This Post Helpful? 0
  • +
  • -

#9 scared0o0rabbit  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 18
  • Joined: 20-September 09

Re: A few questions about using wxWidgets

Posted 27 January 2010 - 01:37 PM

You know, about an hour after I posted that last message I was sitting in class and it just occurred to me to include a public method to do it. Thank you for the confirmation that it should work that way. I'll take a look at that also.

So far I've just sort of been winging it with some of the tutorials for using wxSmith in code::blocks and the stuff I've studied about c++ in the past.

Edit: After playing with it a little bit I was essentially able to get it to work like you suggested. I do have another question I need answered, but it's not really related to what I've asked so far, so I'll start a new thread and this one can be closed as far as I'm concerned.

This post has been edited by scared0o0rabbit: 27 January 2010 - 07:06 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1