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 5:09 pm

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 
Offline

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:

Code:
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();
    gtk_button_set_image(card_button[i],button_image);
    gtk_grid_attach(GTK_GRID(grid),card_button[i],i%board,(i/board)+1,1,1);
    g_signal_connect(G_OBJECT(card_button[i]), "clicked", G_CALLBACK(game_engine), i);
}


game_engine function:
Code:
void game_engine(GtkWidget* widget, gpointer data)
{
    int button_id = (int)data;
    char path[30];
    sprintf(path,"res/%d.png",button_id);
    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:
Quote:
(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.
Greetings,
SirPL


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

Joined: Fri Mar 25, 2011 5:16 pm
Posts: 177
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
http://www.gtkforums.com/viewtopic.php?f=3&t=178138&hilit=+sprintf

2) Use the built-in GLib typecasting macros:
GPOINTER_TO_INT()
GINT_TO_POINTER()

https://developer.gnome.org/glib/2.37/glib-Type-Conversion-Macros.html#glib-Type-Conversion-Macros.synopsis
http://www.gtkforums.com/viewtopic.php?f=3&t=988&p=196067=SIGNALS+II#p196067

Also are you sure that you want:
gtk_grid_attach(GTK_GRID(grid),card_button[i],i%board,(i/board)+1,1,1);


Top
 Profile  
 
 Post subject: Re: Button image - images disappears
PostPosted: Mon Apr 07, 2014 11:48 pm 
Offline
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:
gtk_button_set_image(card_button[i],button_image);


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


Top
 Profile  
 
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: No registered users and 3 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