Gtk4 C - raddio button with image from file


please could any body helps me how to create radiobutton with image.

I have my code:

GtkWidget *radioBtn;
GtkImage * imagePasek;
string g_cestaDoAdresare = "/path/to/image/folder/";
string jmenoObrazku;

radioBtn = gtk_check_button_new();

jmenoObrazku = g_cestaDoAdresare + "pasek.gif";
cout << "obr:" << jmenoObrazku << endl;
imagePasek = (GtkImage *) gtk_image_new_from_file(jmenoObrazku.c_str());
cout << "img created" << endl;
gtk_button_set_child(GTK_BUTTON(radioBtn), (GtkWidget *) imagePasek);

but when last command is called I receive:

(radio_s_tridou:6110): GLib-GObject-WARNING **: 06:39:06.190: invalid cast from 'GtkCheckButton' to 'GtkButton'

(radio_s_tridou:6110): Gtk-CRITICAL **: 06:39:06.191: gtk_button_set_child: assertion 'GTK_IS_BUTTON (button)' failed

thanks for any advice.

CheckButton is not a subclass of Button and has its own set_child() method. So, the line

gtk_button_set_child(GTK_BUTTON(radioBtn), (GtkWidget *) imagePasek);

should be changed to

gtk_check_button_set_child(GTK_CHECK_BUTTON(radioBtn), GTK_WIDGET(imagePasek));

thank you for reply.

I was looking for child of check button,but I didn’t succeed.

I tried anyway. And I have received:

error: use of undeclared identifier 'gtk_check_button_set_child'
        gtk_check_button_set_child(GTK_CHECK_BUTTON(radioBtn), GTK_WIDGET(imagePasek));

You need GTK 4.8 to be able to use gtk_check_button_set_child().

If you have an earlier version of GTK then you’ll need to fake it by packing a check button with no label into a box with an image, though the focus will be a bit wonky.

great! thas working. Thanks

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.