C++ 如何使用GTK向按钮添加图像
我正在尝试将图像添加到带有标签的按钮中,但该图像不显示,而损坏的图像也不显示C++ 如何使用GTK向按钮添加图像,c++,gtk3,linux-mint,C++,Gtk3,Linux Mint,我正在尝试将图像添加到带有标签的按钮中,但该图像不显示,而损坏的图像也不显示 stop_button = gtk_button_new_with_label("stop"); image = gtk_image_new_from_file ("/home/cendit/Escritorio/index.jpeg"); gtk_button_set_image (GTK_BUTTON(stop_button),image); 我尝试了另一条路“file:///home/cendit/Escrit
stop_button = gtk_button_new_with_label("stop");
image = gtk_image_new_from_file ("/home/cendit/Escritorio/index.jpeg");
gtk_button_set_image (GTK_BUTTON(stop_button),image);
我尝试了另一条路“file:///home/cendit/Escritorio/index.jpeg“但是没有成功。这就是你需要做的。”
GtkImage *imagen_pantalla_completa;
GtkWidget *pantalla_completa;
pantalla_completa = gtk_button_new_with_label("");
imagen_pantalla_completa = (GtkImage *)gtk_image_new_from_file("/home/user...");
gtk_button_set_image (GTK_BUTTON(pantalla_completa),(GtkWidget *)imagen_pantalla_completa);
您需要添加此项以显示图像
GtkSettings *default_settings = gtk_settings_get_default();
g_object_set(default_settings, "gtk-button-images", TRUE, NULL);
当我们从GTK+2.x转换到3.x时,默认情况下按钮内的图像不可见。遗憾的是,API还没有被清理以反映这一变化,所以这有点像一个陷阱 如果要显示仅包含图像的按钮,可以使用:
GtkWidget *image = gtk_image_new_from_file ("...");
GtkWidget *button = gtk_button_new ();
gtk_button_set_image (GTK_BUTTON (button), image);
GtkWidget *image = gtk_image_new_from_file ("...");
GtkWidget *button = gtk_button_new_with_label ("...");
gtk_button_set_always_show_image (GTK_BUTTON (button), TRUE);
gtk_button_set_image (GTK_BUTTON (button), image);
另一方面,如果希望按钮中同时包含文本和图像,可以使用:
GtkWidget *image = gtk_image_new_from_file ("...");
GtkWidget *button = gtk_button_new ();
gtk_button_set_image (GTK_BUTTON (button), image);
GtkWidget *image = gtk_image_new_from_file ("...");
GtkWidget *button = gtk_button_new_with_label ("...");
gtk_button_set_always_show_image (GTK_BUTTON (button), TRUE);
gtk_button_set_image (GTK_BUTTON (button), image);
有关更多信息,请参阅的文档。更改GTK设置也会使图标显示在其他按钮上,这不是问题所在。