GTK+ Forums

Discussion forum for GTK+ and Programming. Ask questions, troubleshoot problems, view and post example code, or express your opinions.
It is currently Thu Oct 23, 2014 8:44 am

All times are UTC




Post new topic Reply to topic  [ 3 posts ] 
Author Message
 Post subject: member function as callback function to g_signal_connect
PostPosted: Fri Jan 31, 2014 11:16 am 
Offline

Joined: Sat Dec 21, 2013 10:04 am
Posts: 4
Hi

I have created a simple class for creating gtk main window.

I want to know what is correct way to pass class member function as an argument to G_CALLBACK function?

Thanks

Code:
class MainWindow {
public:
    MainWindow();
    ~MainWindow();
    void onButtonClicked(GtkWidget* button, gpointer* data);
    void showWindow();

private:
    GtkWidget* window;
    GtkWidget* button;
};
MainWindow::MainWindow()
{
    window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
    g_signal_connect(window, "delete_event", G_CALLBACK(gtk_main_quit), NULL);

    button = gtk_button_new_with_label("click here");
    g_signal_connect(button, "clicked", G_CALLBACK(&MainWindow: nButtonClicked), NULL);

    gtk_container_add(GTK_CONTAINER(window), button);
}

MainWindow::~MainWindow()
{
}

void MainWindow::onButtonClicked(GtkWidget* button, gpointer* data)
{
    g_printerr("button clicked\n");
}

void MainWindow::ShowWindow() {
    gtk_widget_show_all(window);
}

int main(int argc, char* argv[]) {
    gtk_init(&argc, &argv);

    MainWindow mainWindow;
    mainWindow.showWindow();

    gtk_main();
    return 0;
}


Top
 Profile  
 
 Post subject: Re: member function as callback function to g_signal_connect
PostPosted: Sat Feb 01, 2014 7:51 am 
Offline
Never Seen the Sunlight

Joined: Mon Apr 28, 2008 5:52 am
Posts: 764
Location: UK
Hello,

There is no direct way to connect a C++ class member functions using the C API to a signal. When a C++ class member function is called a hidden argument is also passed giving information on which class/object the call is going to. There are ways around this by making a proxy function in C which you use as your signal handler. This in turn calls the C++ member function. Here you would need to use the extra "User Data" of the signal to help.

The alternative is to use the C++ bindings for GTK called gtkmm http://www.gtkmm.org/.
In gtkmm your code becomes
Code:
#include <gtkmm.h>

class MainWindow : public Gtk::Window {
public:
    MainWindow();
    virtual ~MainWindow();
    void onButtonClicked();

private:
    Gtk::Button button;
};

MainWindow::MainWindow()
{
    button.set_label("click here");

    button.signal_clicked().connect(sigc::mem_fun(*this, &MainWindow::onButtonClicked));
    add(button);
    show_all_children();
}

MainWindow::~MainWindow()
{
}

void MainWindow::onButtonClicked()
{
    g_printerr("button clicked\n");
}

int main(int argc, char* argv[])
{
    Gtk::Main kit(argc, argv);

    MainWindow mainWindow;

    kit.run(mainWindow);

    return 0;
}

_________________
E.


Top
 Profile  
 
 Post subject: Re: member function as callback function to g_signal_connect
PostPosted: Tue Feb 04, 2014 1:39 pm 
Offline

Joined: Sat Dec 21, 2013 10:04 am
Posts: 4
@errol thanks for your reply.

I got your point regarding hidden this pointer sent to member function call.


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

All times are UTC


Who is online

Users browsing this forum: No registered users and 2 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:  
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group