Re: Writing C++ apps with Gtk?



In C++, instance methods are not quite the same as normal C functions (for
instance, they must know which instance they are bound to).

To fix your problem, make the callback a normal function.  You probably
want to pass a pointer to the instance as the data for the signal.  Then
the signal function can just cast the data argument to (TopLevel *) and
call the method.

The alternative is to use gtk--, which may be preferable in this case.

I hope this helps,

James Henstridge.

--
Email: james@daa.com.au
WWW:   http://www.daa.com.au/~james/


On Thu, 1 Oct 1998, Lars Hoss wrote:

> Hi,
> 
> I am working on a C++ Gtk application.
> Now I have a strange prob with the callback functions.
> The pointer to the widget in the callback function seems to be
> somewhat incorrect?
> 
> Here's a simple example:
> 
> #include <stdio.h>
> #include "toplevel.h"
> 
> TopLevel::TopLevel()
> {
>   GtkWidget *window;
>   GtkWidget *button;
>   
>   window = gtk_window_new( GTK_WINDOW_TOPLEVEL );
>   gtk_container_border_width( GTK_CONTAINER(window), 10 );
> 
>   button = gtk_button_new_with_label( "C++ Test" );
>   gtk_signal_connect( GTK_OBJECT(window), "destroy",
>                       GTK_SIGNAL_FUNC(TopLevel::cbDestroy), this );
>   gtk_signal_connect( GTK_OBJECT(button), "clicked",
>                       GTK_SIGNAL_FUNC(TopLevel::cbHello), this );
> 
>   gtk_container_add( GTK_CONTAINER(window), button );
>   
>   gtk_widget_show(button);
>   gtk_widget_show(window);
> }
> 
> void TopLevel::run()
> {
>   gtk_main();
> }
> 
> void TopLevel::cbHello( GtkWidget *widget, gpointer data )
> {
>   g_print( "width:%d height:%d\n", 
>            widget->allocation.width, 
>            widget->allocation.height );
> }
> 
> void TopLevel::cbDestroy( GtkWidget *widget, gpointer data )
> {
>   printf( "DEBUG: called cbDestroy!\n" );
>   gtk_main_quit();
> }
> 
> The function TopLevel::cbHello is important.
> This width and height aren't the correct values, so the pointer
> must be wrong?
> How can I fix this?
> 
> Thanx for your help,
> Lars
> 
> -- 
> Lars Hoss
> email: Lars.Hoss@munich.netsurf.de
> WWW: http://www.informatik.uni-muenchen.de/~hoss
> 
> 
> -- 
>          To unsubscribe: mail gnome-list-request@gnome.org with 
>                        "unsubscribe" as the Subject.
> 



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