lotfio
lotfio

Reputation: 1936

GTK 3 C++ icon with label in a button

I am trying to get an icon to appear along side with the label in a button but seems like there is an overriding going on and the button is accepting only one element either label or icon.

here is what i have done so far :

#include <gtkmm.h>

int main(int argc, char** argv)
{
    auto app = Gtk::Application::create(argc, argv);

    Gtk::Window* win = new Gtk::Window;

    Gtk::Image* icon = new Gtk::Image;
    icon->set("src/ui/icons/about.png");


    Gtk::Button* btn = new Gtk::Button;

    btn->set_label("Hello world");
    btn->set_image(*icon);

    win->add(*btn);
    win->show_all();

    return app->run(*win);
}

How can i get both icon and label to show in the button ? Thanks !

Upvotes: 1

Views: 1534

Answers (1)

lb90
lb90

Reputation: 958

There is a global-wide Gtk setting, gtk-button-images, that distributions or users can use to style applications look. When the value is false, Gtk hides the images in buttons that also have a label.

You can make your button always show the image, regardless of this system setting, by calling gtk_button_set_always_show_image

Here's the updated sample:

#include <gtkmm.h>

int main(int argc, char** argv)
{
    auto app = Gtk::Application::create(argc, argv);

    Gtk::Window* win = new Gtk::Window;

    Gtk::Image* icon = new Gtk::Image;
    icon->set("src/ui/icons/about.png");


    Gtk::Button* btn = new Gtk::Button;

    btn->set_label("Hello world");
    btn->set_image(*Gtk::manage(icon));
    btn->set_always_show_image(true);

    win->add(*Gtk::manage(btn));
    win->show_all();

    return app->run(*win);
}

Upvotes: 1

Related Questions