 |
GTK+ Forums Discussion forum for GTK+ and Programming. Ask questions, troubleshoot problems, view and post example code, or express your opinions.
|
|
|
| Author |
Message |
|
|
opique Familiar Face
Joined: 19 Feb 2008 Posts: 5
|
Posted: Thu Feb 21, 2008 10:31 am Post subject: (New related problem) Button, entry and label |
|
|
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 |
|
| Back to top |
|
 |
Micah Carrick Never Seen the Sunlight
Joined: 21 Sep 2005 Posts: 462 Location: Portland, OR USA
|
Posted: Sun Feb 24, 2008 6:13 pm Post subject: |
|
|
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. |
|
| Back to top |
|
 |
opique Familiar Face
Joined: 19 Feb 2008 Posts: 5
|
Posted: Wed Feb 27, 2008 9:17 am Post subject: |
|
|
| Ok, thanks for the tips :) I'll try change them out of globals now. |
|
| Back to top |
|
 |
opique Familiar Face
Joined: 19 Feb 2008 Posts: 5
|
Posted: Thu Mar 06, 2008 11:57 am Post subject: problems with const gchar *exampleTable[10] |
|
|
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: (C) | 1 2 3 4 5 6
|
gint i;
i = 0;
exampleTable[i] = exampleWord;
i++;
|
after that when I click button B, it will do this:
| Code: (C) | 1 2 3 4 5 6 7 8 9
|
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: (C) | 1 2 3
|
const gchar *names[] = { "Andrew", "Joe", "Samantha", "Jonathan" };
|
and then save it into the file like this:
| Code: (C) | 1 2 3
|
fprintf(file, " %s \n", names[j]);
| |
|
| Back to top |
|
 |
parsifal
Joined: 02 Sep 2007 Posts: 3
|
Posted: Mon Mar 10, 2008 6:20 pm Post subject: Re: problems with const gchar *exampleTable[10] |
|
|
| opique wrote: |
| Code: (C) | 1 2 3
|
FILE *file = fopen("example.txt", "w");
|
|
You're not opening the text file in append mode. Try:
| Code: (C) | 1 2 3
|
FILE *file = fopen("example.txt", "a");
| |
|
| Back to top |
|
 |
opique Familiar Face
Joined: 19 Feb 2008 Posts: 5
|
Posted: Tue Mar 11, 2008 7:10 am Post subject: |
|
|
| 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 |
|
| Back to top |
|
 |
|
Powered by phpBB © 2001, 2005 phpBB Group CodeBB 1.0 Beta 2
|