FRIHOST FORUMS SEARCH FAQ TOS BLOGS COMPETITIONS
You are invited to Log in or Register a free Frihost Account!


GTK and C++





Antoine_935
Hi everybody.

Well, I have a problem.
In an object, I cannot successfully pass a "this" pointer with the function g_signal_connect
Code:
g_signal_connect(G_OBJECT(button), "clicked", G_CALLBACK(some_function), this);

Code:
void some_function(GtkWidget *widget, gpointer data) {
    g_print("%d", (reinterpret_cast<the_object *>(data))->int_value);
}

This prints 0, even if this->int_value is set to 510, 354, 54534 and so on.
Why ? and how could I pass the this pointer successfuly ?
mogyoro
It's a very tricky question you ask there because I can just guess how you use this code...

Keep it simple!!!

If you would be more specific, like the structure of your program and stuff, maybe I could help...
deepak
Hi Antonie,

I never tried THIS there but i guess following should work.

Code:

obj data;
g_signal_connect(G_OBJECT(button), "clicked", G_CALLBACK(some_function), data);
sergi
I think this will do ( but I'm not 100% sure Confused ):

Code:
void some_function( GtkWidget *widget, gpointer data ){
    g_print( "%d", ((the_object*)data)->int_value );
}


However I suggest you to use the gtkmm (www.gtkmm.org). It is a C++ interface for gtk GUI library. If you are a C++ programmer you'll find it more comfortable and much easier to use instead of native gtk. In this API every widget has its own class thus it is very intuitive and straightforward.
Just take a look (http://www.gtkmm.org/docs/gtkmm-2.4/docs/tutorial/html/apb.html) at the method of signal handler connection.
The signal in gtkmm API can be connected either to a global function or to a class object's method. The code is as follows:

connecting signal to a global function:
Code:
#include <gtkmm/button.h>

void on_button_clicked()
{
    std::cout << "Hello World" << std::endl;
}

main()
{
    Gtk::Button button("Hello World");
    button.signal_clicked().connect(sigc::ptr_fun(&on_button_clicked));
}


connecting signal to an object's method:
Code:

void on_button_clicked();

class some_class
{
public:
    void some_class();
    void on_button_clicked();
};

some_class some_object;

void some_class::some_class()
{
    Gtk::Button button;
    button.signal_clicked().connect( sigc::mem_fun( *this, &on_button_clicked ) );
}


Like in C gtk, multiple functions can be assigned to handle a single signal. Good luck! Laughing
Related topics
Is it possible to compile gtk+ with Tcc instead of Gcc?
Everything object oriented - WHY?!
Funny C variable
What can I do with C-Panel?
HELP ME in C++
Languages in c++
Command and Conquer
C++ TUTORIAL : INTRODUCTION TO C++
I searching for a site
Image Processing in C++ by Shirish
C en C++
C# tutorials
C en C++
C#,Visual Basic.NET,Visual Studio.NET Videos
Reply to topic    Frihost Forum Index -> Scripting -> Others

FRIHOST HOME | FAQ | TOS | ABOUT US | CONTACT US | SITE MAP
© 2005-2011 Frihost, forums powered by phpBB.