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 Aug 25, 2016 7:56 am

All times are UTC

Post new topic Reply to topic  [ 3 posts ] 
Author Message
 Post subject: Button image - images disappears
PostPosted: Tue Jan 28, 2014 10:22 am 

Joined: Mon Jan 27, 2014 11:01 pm
Posts: 1
Hello everyone!
That's my very first post here. First of all I'd like to apologise for my poor english. I really hope you will understand what I try to say.

I'm writing in ANSI-C and using GTK+ 3.6.4 (x32)

Ok, so there's my problem: I have a few buttons generated in loop:

int board = 4;
int board_2 = board*board;
GtkWidget *card_button[board_2];

char path[100];
sprintf(path,"res/0.png"); //path to default image
GtkWidget *button_image = gtk_image_new_from_file(path);

for(int i=0; i<board_2; i++)
    card_button[i] = gtk_button_new();
    g_signal_connect(G_OBJECT(card_button[i]), "clicked", G_CALLBACK(game_engine), i);

game_engine function:
void game_engine(GtkWidget* widget, gpointer data)
    int button_id = (int)data;
    char path[30];
    GtkWidget *button_image = gtk_image_new_from_file(path);
    gtk_button_set_image(widget, button_image);

When I run this code everything seems to be ok, but sometime, when I click a button, image shows up on clicked button, but disappear on another (there's no rule which image disappear). Moreover on console I see:
(Program.exe:4920): Gtk-CRITICAL **: gtk_widget_get_parent: assertion 'GTK_IS_WIDGET (widget)' failed

I used debbuger and found that gtk_button_set_image from game_engine function causes this error message.
I don't know what to do next. Thank you in advance for help.

 Post subject: Re: Button image - images disappears
PostPosted: Mon Mar 10, 2014 1:21 am 
GTK+ Guru

Joined: Fri Mar 25, 2011 5:16 pm
Posts: 181
Location: USA
This might not be correct but I'll give it a try:

1) There are buffer overflow and security concerns with using sprintf
Instead use g_snprintf

2) Use the built-in GLib typecasting macros:

Also are you sure that you want:

 Post subject: Re: Button image - images disappears
PostPosted: Mon Apr 07, 2014 11:48 pm 
Familiar Face

Joined: Wed Jan 30, 2013 8:28 pm
Posts: 20
there is a problem in your first code into the for loop:

for each call to the following function in this for loop there is a problem:

the first time the loop is executed the "button_image" widget is made as a child of card_button[0]

when the loop is executed again the card_button[1] try to make "button_image" as its child (when setting the image on this button) but
"button_image" is already a child of card_button[0] so this will not work!!!

who have to create a number of image which will be the same number as the button

so in the loop when creating a button, who also have to create a new image widget for it

Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 3 posts ] 

All times are UTC

Who is online

Users browsing this forum: Yahoo [Bot] and 2 guests

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