Re: [gtk-list] Set Label Text



Andreas Scherf wrote:
>void 
>ndisplay_text( GtkWidget *label, gchar *text, guint sec )
>{
>	char *tmpstring;
>
>	gtk_label_get( GTK_LABEL( label ), &tmpstring );
>	gtk_label_set( GTK_LABEL( label ), text );
>	gtk_widget_show( label );
>
>	sleep( sec );
>
>	gtk_label_set_text( GTK_LABEL( label ), tmpstring );
>}

Hi Andreas, the problem is that the old string is being freed when the new
string is set. You want to save a copy of it somewhere. For example:

void 
ndisplay_text( GtkWidget *label, gchar *text, guint sec )
{
	char *tmpstring;
	char *save;

	gtk_label_get( GTK_LABEL( label ), &tmpstring );
	save = g_strdup( tmpstring );

	gtk_label_set( GTK_LABEL( label ), text );
	gtk_widget_show( label );

	sleep( sec );

	gtk_label_set_text( GTK_LABEL( label ), save );
	g_free( save );
}

Another improvement is to get rid of the sleep() call. Your application will
"freeze" while the sleep is running. A better way is to make a timer callback
for sec seconds in the future to reset the message to the old value.

An even better way is to use the gtkstatusbar widget :). It does most of this
stuff for you, you just need to add the timer callback. Happy hacking!

John
--
John Cupitt, john.cupitt@ng-london.org.uk, +44 (0)171 930 2108
VASARI Lab, The National Gallery, Trafalgar Square, London, WC2N 5DN



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]