cancel
Showing results for 
Search instead for 
Did you mean: 

How to load glade XML file in user space project created in the STM32 Cube IDE?

MSale.1
Senior

I am trying to build gui using GLADE designer for STM32 MP157 DK but i have problem in loading the generated GLADE XML in user space project.

1 ACCEPTED SOLUTION

Accepted Solutions
JMala.3
Associate III

Yes I understand. The problem is function call gtk_builder_add_from_file (GtkBuilder *builder, const gchar *filename GError **error ); Expects a filename that can be looked up in a file system.

Since you do not have a file system, you need to be using gtk_builder_add_from_string (GtkBuilder *builder const gchar *buffer gsize length GError **error ); which expects a pointer to character array with the array size of the character array. Follow these steps

Take the GLADE file you already have (I think you callled it "mygui.glade") and make is into a C array using the srec_cat utility, with the command line

srec_cat mygui.glade -Binary -o mygui.h -C-Array mygui_glade

This will produce a file called mygui.h containing a C array called mygui_glade There will also be a constant defining the size in the same H file

Now include the H file in your C code, and add the call gtk_builder_add_from_string (Builder, mygui_glade, mygui_glade_size, error); in all places where you called gtk_builder_add_from_file previously,

View solution in original post

4 REPLIES 4
JMala.3
Associate III

Convert it to a C array using srec_cat.exe, a free utility, available for Linux and windows. I use this to include TLC certificates in my project, using the command

srec_cat client-cert.pem -Binary -o client_cert.h -C-Array client_cert_pem

This converts the binary file "client-cert.pem" file into a C array called client_cert_pem in the file client_cert.h

Just substitute the name of your XML file, and include the H file in any of your C files.

@JMala.3​ 

Hi,

Thanks for your reply. this is the code and its from example from youtube. the problem is in this part:

if(0 == gtk_builder_add_from_file (builder, "myui.glade", &err))

{

  /* Print out the error. You can use GLib's message logging */

  fprintf(stderr, "Error adding build from file. Error: %s\n", err->message);

  /* Your error handling code goes here */

}

i created the file using glade designer and now i want to add it in stm32 cube ide user space project.

// My Application

//

// By (Your name)

// Libraries

#include <gtk-3.0/gtk/gtk.h>

#include <stdio.h>

// Variables

GtkWidget *window; // Window

GtkLabel *mylabel; // Label

GError *err = NULL; /* It is mandatory to initialize to NULL */

// Main function

int main(int argc, char *argv[])

{

GtkBuilder *builder; // GTK Builder variable

gtk_init(&argc, &argv); // Start GTK

builder = gtk_builder_new(); // Create GTK UI Builder

//gtk_builder_add_from_file(builder, "myui.glade", NULL); // Load our UI file

if(0 == gtk_builder_add_from_file (builder, "myui.glade", &err))

{

  /* Print out the error. You can use GLib's message logging */

  fprintf(stderr, "Error adding build from file. Error: %s\n", err->message);

  /* Your error handling code goes here */

}

// Assign the Variables

window = GTK_WIDGET(gtk_builder_get_object(builder, "MyWindow")); // Load our window named MyWindow

mylabel = GTK_LABEL(gtk_builder_get_object(builder, "MyLabel")); // Load our label named MyLabel

// Essential for a GTK based program

gtk_builder_connect_signals(builder, NULL);

g_object_unref(builder);

gtk_widget_show_all(window); // Show our window

gtk_main(); // Run our program

return 0; // Necessary for a c main function

}

// Function to exit our app

void exit_app()

{

printf("Exit app \n"); // Not neccesary

gtk_main_quit(); // Command to quit a GTK program

}

// Function when our button is pressed

void button_clicked()

{

printf("Button clicked \n");

gtk_label_set_text(mylabel, "Hello World");

}

JMala.3
Associate III

Yes I understand. The problem is function call gtk_builder_add_from_file (GtkBuilder *builder, const gchar *filename GError **error ); Expects a filename that can be looked up in a file system.

Since you do not have a file system, you need to be using gtk_builder_add_from_string (GtkBuilder *builder const gchar *buffer gsize length GError **error ); which expects a pointer to character array with the array size of the character array. Follow these steps

Take the GLADE file you already have (I think you callled it "mygui.glade") and make is into a C array using the srec_cat utility, with the command line

srec_cat mygui.glade -Binary -o mygui.h -C-Array mygui_glade

This will produce a file called mygui.h containing a C array called mygui_glade There will also be a constant defining the size in the same H file

Now include the H file in your C code, and add the call gtk_builder_add_from_string (Builder, mygui_glade, mygui_glade_size, error); in all places where you called gtk_builder_add_from_file previously,

@JMala.3​ 

Thank you. your solution worked