Thread: Passing data through callback functions [GTK+ 2.0]

  1. #1
    Registered User
    Join Date
    Sep 2012
    Posts
    43

    Passing data through callback functions [GTK+ 2.0]

    Hello everyone.

    I realize this isn't a GTK forum, but I am still hoping that someone might know how to get this going.

    I used glade to make a GTK GUI. It has several buttons and each one has its own callback function. Meaning that my program is running on an infinite cycle. When a clicked-button signal is sent, the function associated to that signal is executed.

    I'd like to know what is the best way to pass data through these functions

    The following code happens when I press an "OK" button in a File Chooser Dialog

    Code:
    void OK_FCD_clicked ()
    {
        int n;
        GSList * FileList;
        
    
        
         GtkWidget *filechooserdialog =  GTK_WIDGET(gtk_builder_get_object(builderG, "filechooserdialog1")); //  gets the widget from the builder object
        
        FileList =  gtk_file_chooser_get_filenames(GTK_FILE_CHOOSER(filechooserdialog));  //puts the selected files path in the GSlist FileList
        
        guint nFiles = g_slist_length(FileList); //number of files selected
        
        PathList paths[nFiles]; //struct I created to organize the data
        
        for (n=0; n<nFiles;n++) //cycle to put the GSlist data into my struct
        {
    
         paths[n].number=n;  
         paths[n].filename=g_slist_nth_data(FileList,n);
         paths[n].total=nFiles;
        }
        
        
        for (n=0; n<nFiles;n++) printf("%d -  %s\n",paths[n].number,paths[n].filename); //this cycle just prints out  the information I just organized
     
    }
    The PathList struct is the following:

    Code:
    typedef struct {int number; char * filename; int total;} PathList;
    Now I need to use that information in other callback functions, but I am not sure how to pass the data through all these functions.

    I realize it is possible to do it with a global variable, but I'm thinking that one is a poor solution.

    Best regards

  2. #2
    Registered User
    Join Date
    Dec 2012
    Posts
    45
    I googled "gtk callback" and found some code for installing a callback. I saw that the callback function received a "gpointer". They didn't use it in this example, so I googled "gtk callback gpointer" and found this:

    https://mail.gnome.org/archives/gtk-list/1998-October/msg00334.html


    I hope it helps ;-)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Passing data between functions
    By john7 in forum C Programming
    Replies: 6
    Last Post: 05-25-2011, 08:25 AM
  2. Passing structs to callback functions (GTK+ related)
    By Raskalnikov in forum C Programming
    Replies: 2
    Last Post: 03-21-2009, 12:46 PM
  3. passing data between functions
    By nesagsar in forum C++ Programming
    Replies: 11
    Last Post: 12-08-2006, 12:48 PM
  4. functions and passing data
    By redmondtab in forum C Programming
    Replies: 41
    Last Post: 09-21-2006, 12:04 PM
  5. Passing data/pointers between functions
    By TankCDR in forum C Programming
    Replies: 1
    Last Post: 11-02-2001, 12:59 AM