LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 02-22-2006, 11:02 AM   #1
Nylex
LQ Addict
 
Registered: Jul 2003
Location: London, UK
Distribution: Slackware
Posts: 7,464

Rep: Reputation: Disabled
Question about GTK+ and global variables


Hi people, I have a user interface with several text entries (though the data entered will be numeric) and a button. When that button is pressed, I need to get all the data that the user has entered and perform calculations with it. Obviously, if I decare those pointers in main(), they'll be local to that function and I won't be able to use them in my button's callback function. The only way I can see to get round this is to declare those pointers with global scope and then do the calculations and stuff in the callback function. Is that the only way to do what I need, or is there a better way?

Thanks.
 
Old 02-22-2006, 11:26 AM   #2
paulsm4
LQ Guru
 
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Blog Entries: 1

Rep: Reputation: Disabled
Hi -

Globals is one way - it's very often the simplest, cleanest and most efficient way.

Your other choice is to put the data in a struct or C++ class, then pass a pointer in your callback.

I use the C API (i.e. GTK+, not gtkmm) in C++ programs, and I usually take the "pass the callback a reference to my C++ class" approach.
 
Old 02-22-2006, 11:35 AM   #3
Nylex
LQ Addict
 
Registered: Jul 2003
Location: London, UK
Distribution: Slackware
Posts: 7,464

Original Poster
Rep: Reputation: Disabled
Nice one, cheers Paul. A class seems good to use, so I shall look into that .
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
g++ complains about global variables copolii Programming 3 09-18-2005 08:43 PM
Local vs Global variables wujee Programming 1 03-11-2005 11:43 PM
global variables in php champ Programming 2 10-13-2002 01:31 PM
exporting global variables neo77777 Programming 6 09-18-2002 01:46 PM
global environmental variables antadam Linux - General 2 10-11-2001 06:36 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 01:59 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration