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 3:51 am

All times are UTC




Post new topic Reply to topic  [ 10 posts ] 
Author Message
 Post subject: Pass const char* to g_signal_connect
PostPosted: Tue Jan 31, 2012 3:34 pm 
Offline
Familiar Face

Joined: Tue Jan 31, 2012 3:08 pm
Posts: 7
Hello!

I have problems with passing a const char* to g_signal_connect.
I have a function run(gpointer *data)

and a tool item "button"
GtkToolItem *button = gtk_tool_button_new_from_stock(GTK_STOCK_ABOUT);

which should call the function "run" when it is clicked and which is connected by
g_signal_connect(button, "clicked", G_CALLBACK(run), (gpointer*)&data);

the param "data" usually is of type const char*.

I need to process "data" in the function "run", but I don't know how to cast or dereference them correctly.
When I try to print "data" in the function "run" with
g_print(%s\n", (gchar*)data);
the program prints out some weird characters.

Can anybody help what I'm doing wrong? I don't know how to cast const char* to gpointer and back.

Thank you very much for your help!


Top
 Profile  
 
 Post subject: Re: Pass const char* to g_signal_connect
PostPosted: Tue Jan 31, 2012 6:47 pm 
Offline
Familiar Face

Joined: Sun Jan 01, 2012 7:28 pm
Posts: 14
Location: Qu├ębec
Hi sabine. I am new here too, so bear with me as I take a stab at your problem.
By the way, I was involved in a similar problem. If you haven't seen that thread, it migh give you some insights understanding g_signal_connect

I think your signal connect call should be like so:
Code:
g_signal_connect(button, "clicked", G_CALLBACK(run), (gpointer)data);

And to print the data:
Code:
g_print("%s\n", data); //You are missing a " before the %
.


Top
 Profile  
 
 Post subject: Re: Pass const char* to g_signal_connect
PostPosted: Tue Jan 31, 2012 7:38 pm 
Offline
Never Seen the Sunlight

Joined: Wed Jul 23, 2008 10:31 am
Posts: 2406
Location: Slovenia
Hello and welcome to the GTK+ forums.

I quickly wrote down minimal application that demonstrates general principle of how passing data to callback works. Most important thing is to have the right function prototype (proper prototype is documented in API docs of every signal). And here is the code:

Code:
#include <gtk/gtk.h>

static void
cb_print (GtkButton   *button,
          gchar const *data)
{
  g_print ("%s\n", data);
}

int
main (int    argc,
      char **argv)
{
  GtkWidget *window,
            *button;
  gchar const *data = "Sample string";


  gtk_init (&argc, &argv);

  window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
  g_signal_connect (window, "destroy", gtk_main_quit, NULL);

  button = gtk_button_new_with_label ("Print to console");
  g_signal_connect (button, "clicked",
                    G_CALLBACK (cb_print), (gpointer)data);
  gtk_container_add (GTK_CONTAINER (window), button);

  gtk_widget_show_all (window);

  gtk_main ();

  return 0;
}


Cheers,
Tadej


Top
 Profile  
 
 Post subject: Re: Pass const char* to g_signal_connect
PostPosted: Wed Feb 01, 2012 9:34 am 
Offline
Familiar Face

Joined: Tue Jan 31, 2012 3:08 pm
Posts: 7
Hello!

Thank you very much for your posts!!
I could solve the problem partly. It seems to be a pointer problem.

If gchar const* gets a sequence of chars in function_1 directly, everything works fine:

Code:
static void cb_print (GtkButton   *button, gchar const *data)
{
  g_print ("%s\n", data); // works fine
}

void function_1(){
   gchar const *data = "Test String";
        g_print("%s \n", data); // works fine
   
        GtkToolItem *notification = gtk_tool_button_new_from_stock(GTK_STOCK_ABOUT);
   g_signal_connect(notification, "clicked", G_CALLBACK(run), (gpointer)data);
   gtk_widget_show((GtkWidget*)notification);
   
}


But if gchar const* data is a param of function_1, cb_print still prints weird symbols:

Code:
static void cb_print (GtkButton   *button, gchar const *data)
{
  g_print ("%s\n", data); // prints weird symbols
}

void function_1(gchar const *data){
   g_print("%s \n", data); // works fine
   
        GtkToolItem *notification = gtk_tool_button_new_from_stock(GTK_STOCK_ABOUT);
   g_signal_connect(notification, "clicked", G_CALLBACK(run), (gpointer)data);
   gtk_widget_show((GtkWidget*)notification);
   
}


I don't know what I'm doing wrong. Is there a problem with the gpointer cast? Or with dereferencing?


Top
 Profile  
 
 Post subject: Re: Pass const char* to g_signal_connect
PostPosted: Wed Feb 01, 2012 9:46 am 
Offline
Never Seen the Sunlight

Joined: Wed Jul 23, 2008 10:31 am
Posts: 2406
Location: Slovenia
Hello.

You're having troubles with C, not with GTK+ here. Let me help you a bit with this code that has one huge error in it. If you find it here, you'll know what is going wrong with your GTK+ application.

Code:
#include <stdio.h>

static char const *
get_some_text (void)
{
  char const text[] = "Sample text";

  printf ("1: %s\n", text);

  return text;
}

int
main (int    argc,
      char **argv)
{
  char const *text = get_some_text ();

  printf ("2: %s\n", text);

  return 0;
}


Cheers,
Tadej


Top
 Profile  
 
 Post subject: Re: Pass const char* to g_signal_connect
PostPosted: Thu Feb 02, 2012 10:01 am 
Offline
Familiar Face

Joined: Thu Dec 15, 2011 6:23 pm
Posts: 36
Location: Hrvatska
Nice example Tadej !

Explanation:
1) Try to find error in Tadej code
2) Here is the answer:
The text variable in get_some_text function is local, not preserved out of function.
That's why pointer to this variable is useless. it has to point to some global variable.


Top
 Profile  
 
 Post subject: Re: Pass const char* to g_signal_connect
PostPosted: Thu Feb 02, 2012 11:56 am 
Offline
Familiar Face

Joined: Tue Jan 31, 2012 3:08 pm
Posts: 7
It's clear to me that char const text[] is a local variable and therefore the pointer is useless.

But why can't I just pass this pointer from function_1 to cb_print?
Code:
static void cb_print (GtkButton   *button, gchar const *data)
{
  g_print ("%s\n", data); // prints weird symbols
}

void function_1(gchar const *data){
   g_print("%s \n", data); // works fine
   
        GtkToolItem *notification = gtk_tool_button_new_from_stock(GTK_STOCK_ABOUT);
   g_signal_connect(notification, "clicked", G_CALLBACK(run), (gpointer)data);
   gtk_widget_show((GtkWidget*)notification);
   
}


Sorry, I don't see the error my example. It also doesn't work with a global variable.


Top
 Profile  
 
 Post subject: Re: Pass const char* to g_signal_connect
PostPosted: Thu Feb 02, 2012 8:02 pm 
Offline
Never Seen the Sunlight

Joined: Wed Jul 23, 2008 10:31 am
Posts: 2406
Location: Slovenia
Well, to give you a specific answer (and conformation that your code is OK), you'll need to tell us what exactly data is and how and where is defined.

Cheers,
Tadej


Top
 Profile  
 
 Post subject: Re: Pass const char* to g_signal_connect
PostPosted: Wed Feb 08, 2012 7:57 am 
Offline
Familiar Face

Joined: Tue Jan 31, 2012 3:08 pm
Posts: 7
Well, the situation is the following:
I have two classes, the first one is a video player which receives MPEG video and audio data as well as a data stream.
The second class defines the gtk GUI for the player.

If the player receives a data packet, this packet is passed to the GUI as const char*. Initially, the player receives the data packet
as uint8_t*.
After that, it is basically the code I have already posted. Function_1 in GUI class gets the const char* (or gchar const*, doesn't matter) data packet from the player.
In function_1 everything works fine. The problem is when I try to pass data to cb_print or when I try to pass data to a global const char* (or gchar const*, doesn't matter) variable
that is defined in the GUI class. It even doesn't work when I create a new const char* (or gchar const*, doesn't matter) in function_1 and assign the passed data and pass this
new defined pointer to cb_print.

Code:
static void cb_print (GtkButton   *button, gchar const *data)
{
  g_print ("%s\n", data); // prints weird symbols
}

void function_1(gchar const *data){
   g_print("%s \n", data); // works fine
   
        GtkToolItem *notification = gtk_tool_button_new_from_stock(GTK_STOCK_ABOUT);
   g_signal_connect(notification, "clicked", G_CALLBACK(run), (gpointer)data);
   gtk_widget_show((GtkWidget*)notification);
   
}


I hope it's clear!


Top
 Profile  
 
 Post subject: Re: Pass const char* to g_signal_connect
PostPosted: Fri Feb 24, 2012 2:36 pm 
Offline
Familiar Face

Joined: Tue Jan 31, 2012 3:08 pm
Posts: 7
I "solved" the issue by using a global string variable.

Br,
Sabine


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

All times are UTC


Who is online

Users browsing this forum: Google [Bot] and 1 guest


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:  
cron
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group