Results 1 to 5 of 5

Thread: GTK+ binding signal to C++ member function

  1. #1
    Join Date
    Apr 2007
    Beans
    116

    GTK+ binding signal to C++ member function

    Ive encapsulated a GTK+ window and all its widgets into a class called SubsystemEditWindow and I want an instance method / member function called doSave() to be called when the Save button in the window is clicked.

    If I do g_signal_connect(G_OBJECT(this->savebutton),"clicked",G_CALLBACK(&SubsystemEditWi ndow::doSave),NULL);

    it calls the function code but not in the object instance concerned - all instance varaibles are set to null!

    is there a way to make it point to a particular instance? i tried

    g_signal_connect(G_OBJECT(this->savebutton),"clicked",G_CALLBACK(&this->doSave),NULL);

    but that wont compile.

    Thanks

  2. #2
    Join Date
    Jun 2007
    Location
    Maryland, US
    Beans
    6,285
    Distro
    Kubuntu

    Re: GTK+ binding signal to C++ member function

    To call a non-static class function/method, you need to convey to GTK a pointer to the instance of the class.

    I am not familiar with GTK, so I am not sure how to do this. From this webpage, it appears that the GTK-function you are attempting to use has a signature as follows:
    Code:
    #define g_signal_connect (instance, detailed_signal, c_handler, data)
    P.S. Upon doing a little more research, it would seem that you need to pass your object's instance as the 'data' parameter, instead of NULL, as you are passing now. The callback function needs to be declared as static. Once inside it, use the 'data' parameter as the instance to your object.

    Code:
    class MyClass
    {
    public:
       MyClass()
       {
          g_signal_connect(G_OBJECT(this->savebutton), "clicked", G_CALLBACK(&this->doSaveCallback), this);
       }
    
       static void doSaveCallback(GstElement* element, GstPad* pad, MyClass* data)
       {
          data->doSave();
       }
    private:
       void doSave()
       {
          ...
       }
    };
    Last edited by dwhitney67; November 22nd, 2009 at 06:03 PM.

  3. #3
    Join Date
    Apr 2007
    Location
    (X,Y,Z) = (0,0,0)
    Beans
    3,715

    Re: GTK+ binding signal to C++ member function

    Er... wouldn't be better to use GTKmm (GTK+ C++ binding) instead?

  4. #4
    Join Date
    Apr 2007
    Beans
    116

    Re: GTK+ binding signal to C++ member function

    thats great thanks. i guess gtkmm might be a better long term solution but ill stick with gtk+ for now

  5. #5
    Join Date
    Apr 2007
    Beans
    116

    Re: GTK+ binding signal to C++ member function

    Is it possible to mix gtkmm and gtk+ in the same program? I donbt really want to have to re-write everything else to use gtkmm.

    im thinking about the calls to Gtk::Main::run(); versus gtk_main()

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •