GTK+ Forums

Discussion forum for GTK+ and Programming. Ask questions, troubleshoot problems, view and post example code, or express your opinions.
It is currently Sat Nov 01, 2014 7:34 am

All times are UTC




Post new topic Reply to topic  [ 6 posts ] 
Author Message
 Post subject: (New related problem) Button, entry and label
PostPosted: Thu Feb 21, 2008 10:31 am 
Offline
Familiar Face

Joined: Tue Feb 19, 2008 10:26 am
Posts: 5
Edit

Well solved it by changing the widgets as global, but still not happy with the result. Guess I'm not familiar enough with pointers. The problem was that because all my widgets are in Main, my callbacks couldn't see the other widgets than the one they were calling back. :P Well that probably makes no sense whatsoever..

/Edit

Hi,

Sorry for the newbie question, but I have not been able to find a solution on how do this:

Let's say I have a button, a text entry and a label. When I type something in the text entry and then click the button, how do I get the label to display what was written?

Thanks for the answer beforehand and thank you for this awesome forum. :)


Last edited by opique on Thu Mar 06, 2008 11:59 am, edited 1 time in total.

Top
 Profile  
 
 Post subject:
PostPosted: Sun Feb 24, 2008 6:13 pm 
Offline
Never Seen the Sunlight

Joined: Wed Sep 21, 2005 12:07 am
Posts: 563
Location: Portland, OR USA
This is a common problem for new C/GTK+ programmers. There's a few ways we do it, however, globals is seldom the solution.

Your callback functions can have "user data" passed to them. When the callback function needs to explicitly access only a single widget, we simply pass the pointer to that widget as user_data in the g_signal_connect() function.

However, if your callback function needs to be able to access more that one widget or variable, you can pass it a struct. This is probably the most common way for small/medium sized applications. An example of this method can be seen in this code: Execute command into a GtkTextView (C and Libglade)

Large/advanced applications will often have custom GObjects for the various parts of the application (preferences, window, dialogs, etc.) and can pass those to callbacks as user_data.

_________________
Micah Carrick - Forum Administrator
http://www.micahcarrick.com


Top
 Profile  
 
 Post subject:
PostPosted: Wed Feb 27, 2008 9:17 am 
Offline
Familiar Face

Joined: Tue Feb 19, 2008 10:26 am
Posts: 5
Ok, thanks for the tips :) I'll try change them out of globals now.


Top
 Profile  
 
 Post subject: problems with const gchar *exampleTable[10]
PostPosted: Thu Mar 06, 2008 11:57 am 
Offline
Familiar Face

Joined: Tue Feb 19, 2008 10:26 am
Posts: 5
I've now ran into another problem related to these same entry fields etc. and was unable to find any help from the net.

Problem description:

Text is entered into a TextEntry, and when a button is clicked this data should be first saved into a ListStore (Treeview) and when another button is clicked, the data from this List should be saved into an empty text file.

The actual problem occurs when I'm saving the data to the text file. Let's say I have the following global variable: const gchar *exampleTable[10]; which is used for saving words and another const gchar *exampleWord; I save the words from the text entry to the exampleWord variable and then display them in a tree by appending the tree every time button A is clicked, I also save these words into the table every time the button A is clicked.

Like this:

Code:
gint i;
i = 0;
exampleTable[i] = exampleWord;
i++;


after that when I click button B, it will do this:

Code:
FILE *file = fopen("example.txt", "w");
int j;      
for(j = 0; j < i; j++)
   {
      fprintf(file, " %s", exampleTable[j]);
   }
   fclose(file);


And when I do that and open the example.txt it will only show the last word I entered, sometimes it will show three times the same last word and it always skips the first three words I've entered.

Any idea what I'm doing wrong here? Let me know if more code or description is required.

Added info:

If I use a variable like this one it works like a charm:


Code:
const gchar *names[] = { "Andrew", "Joe", "Samantha", "Jonathan" };


and then save it into the file like this:

Code:
fprintf(file, " %s \n", names[j]);


Top
 Profile  
 
 Post subject: Re: problems with const gchar *exampleTable[10]
PostPosted: Mon Mar 10, 2008 6:20 pm 
Offline

Joined: Sun Sep 02, 2007 3:42 am
Posts: 3
opique wrote:
Code:
FILE *file = fopen("example.txt", "w");



You're not opening the text file in append mode. Try:

Code:
FILE *file = fopen("example.txt", "a");


Top
 Profile  
 
 Post subject:
PostPosted: Tue Mar 11, 2008 7:10 am 
Offline
Familiar Face

Joined: Tue Feb 19, 2008 10:26 am
Posts: 5
Thanks for the tip, unfortunately it did not help. It's still skipping the first three and now actually the last three for some reason :P


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 6 posts ] 

All times are UTC


Who is online

Users browsing this forum: No registered users and 4 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
cron
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group