GTK Glade Get Pointers to Widgets

How to get pointers to widgets in a GTK Glade C program without using global variables. Use a local structure to pass widget pointers to a callback function. Pass user data and widget pointers to callback.

GTK Glade Pointers to Widgets Example Program and Tutorial

This Glade GTK+ 3 C programming tutorial shows how to get pointers to widgets and / or user data without using global variables. A simple application is used to demonstrate how to pass pointers to two label widgets to a callback function.

Get Pointers to Widgets Glade GTK Application
Get Pointers to Widgets in a Glade GTK Application

The GTK Glade application shown above displays the local time and number of times that the Update button was clicked. Local time and a button click count are refreshed in the window each time that the button is clicked. The button clicked callback handler function needs pointers to both of the label widgets in order to update both of them. Tutorial steps below show how to build this application.

Two previous tutorials showed how to get pointers to labels using global variables and how to pass a pointer to a single widget to a callback function in Glade. This tutorial covers the case where more than one pointer to a widget must be passed to a callback function. Other user data can be passed to the callback in the same way as demonstrated here.

GTK Glade Get Pointers to Widgets Tutorial

Start a new GTK Glade project by copying the GTK Glade C program template files. Rename the project folder and the project name in the makefile to pointer_struct. The make file should now look as follows.


1. Lay Out the Application Window in Glade

The finished file from the project template glade folder is shown below. Copy this file or follow the steps below to create the file yourself.


To build the above Glade file starting with the blank template file, open the project Glade file in the Glade editor and make the following changes.

Change Window Size and Title

Select the application main window in Glade. Uncheck the default width and height check boxes which were set in the template Glade file. Rename the application title to Widget Pointer Struct.

Place a GtkBox Container on the Main Window

Place a GtkBox on the main window with 3 items. Place a label in the top box of the GtkBox and a second label in the second box. Place a button in the third box.

Change the spacing of GtkBox box1 to 10. Change all four margins of the box to 10.

Edit Widget IDs and Text

Change the label and button IDs as shown in the image from the Glade editor below.

Widgets Placed in the Glade Main Window
Widgets Placed in the Glade Main Window

Change the label and button text as shown in the image from the Glade editor below.

Application Window in Glade
Application Window in Glade

Add the Button Clicked Callback Function in Glade

Select the button in Glade and add the callback function to it called on_btn_update_clicked() as shown below.

Button Callback Function in Glade
Button Callback Function in Glade

If you have any doubts about how to lay out the Glade window, refer to the Glade Packing tutorial which is very similar. Alternatively, copy the above glade code and save it to file, open the file in Glade and view the various widget settings.

2. Modify the C Code

Below is the full source code listing for this tutorial found in the src folder of the project.


Passing Widget Pointers to the Callback Function

At the top of the code a structure is defined that contains pointers to the two widget labels. This is a custom structure and can contain any widget pointers or other user data that you would like to pass to a callback function.

Near the top of main(), memory is allocated to the custom structure using the glib function g_slice_new().

The two widget pointer variables in the structure are then initialized with pointers to the two label widgets.

When the callback or handler function set up in Glade (on_btn_update_clicked()) is connected to the button clicked signal using the gtk_builder_connect_signals() function, the widget structure is passed to this function.

Now a pointer to the widgets structure will be passed to the button clicked handler as the user data parameter. Note in the above line of code that the second parameter passed to the builder connect signals function is the widgets structure. In previous tutorials, such as the simple hello world application, this parameter was set to NULL.

Accessing Glade Widgets in the Callback Function

Inside the callback function for the button clicked signal, the structure containing the widget pointers is accessed as the second parameter in the function’s arguments list.

Both labels are updated with new text inside the button callback function. Glib functions are used to get the current time and convert it to a string to be displayed in the first label. A count value is converted into a string to be displayed in the second label. Memory that was dynamically assigned by the glib functions is then freed before the callback exits.

3. Build and Run the Program

Open a command line terminal window and change to the the project directory. Enter make at the command prompt to build the project. Run the program by entering ./pointer_struct at the command prompt.

When the application runs, click the Update button to refresh the time and update the button clicked count.

Resources and Software Tool Details

Refer to the GTK Glade C Programming index to get started with GTK Glade C projects.

Software development environment and tools: This project was developed on a Linux Mint 18 Mate 64-bit system using the following tools.

  • Glade version 3.18.3
  • GTK+3 version 3.18.9
  • GCC version 5.4.0


3 thoughts on “GTK Glade Get Pointers to Widgets”

Leave a Reply

Your email address will not be published. Required fields are marked *