10

I'm trying to change the background color of a button to red but it doesn't seem to work. I paste the example code. If anybody can tell me how to fix my code please help.

#include <gtkmm.h>

// g++ simple.cc -o simple `pkg-config gtkmm-3.0 --cflags --libs`

int  main(int argc, char *argv[])
{
    Gtk::Main kit(argc, argv);
    Gtk::Window  window;
    Gtk::Button  button("TEST");
    button.override_background_color(Gdk::RGBA("red"));
    window.add(button);
    window.show_all();
    Gtk::Main::run(window);
    return EXIT_SUCCESS;
}

UPDATE: ok here's how I solved:

mr_screen = Gdk::Screen::get_default();
mr_style_context = mp_window->get_style_context();
mr_css_provider = Gtk::CssProvider::create();
mr_css_provider->load_from_path(Glib::build_filename(m_glade_dir_path, "filename.css"));
mr_style_context->add_provider_for_screen(mr_screen, mr_css_provider, GTK_STYLE_PROVIDER_PRIORITY_USER);

and the content of filename.css is:

column-header .button {
    background-image: -gtk-gradient (linear,
        left top,
        left bottom,
        from (#51cccc),
        color-stop (0.5, darker (#51cccc)),
        to (#51cccc));
}
giuspen
  • 1,365
  • 3
  • 20
  • 33
  • 2
    I don't see anything wrong in the code, I think it has most likely to do with you theme. If you can set the default theme you can see the change in the button colour. – another.anon.coward Jan 21 '12 at 13:33
  • I didn't think that a gtk3 theme can block the background of a button to be changed – giuspen Jan 21 '12 at 20:27
  • The background of buttons can be changed at least of the latest releases. The key point for me in getting this working was specifying GTK_STYLE_PROVIDER_PRIORITY_USER when adding a provider. Otherwise background-image seem to take priority over background-color – Spacen Jasset Nov 12 '15 at 19:51

2 Answers2

5

Just indeed, I learned it the hard way, that CssProvider is the right way to handle such things. You don't need to use glade, even good is to have css in your code, like so

#include <gtkmm.h>

Gtk::Button* button = new Gtk::Button("Test");
Glib::RefPtr<Gtk::CssProvider> css_provider = Gtk::CssProvider::create();
css_provider->load_from_data(
    "button {background-image: image(cyan);}\
     button:hover {background-image: image(green);}\
     button:active {background-image: image(brown);}");
button->get_style_context()->add_provider(
    css_provider, GTK_STYLE_PROVIDER_PRIORITY_USER);

You have to deal with background-image, because background-color is overlapped from the default background-image and thus not changed. I have my wisdom from Ruby-GNOME2

Erich Kuester
  • 460
  • 4
  • 11
  • Have you any idea where to find any kind of documentation? Even if I have the idea that I have to use CSS for a simply bg color change, how should I find that I have to use background-image instead of of background-color? – Klaus Mar 04 '20 at 18:26
  • @Klaus In an earlier search I found some information in the GTK+ documentation, see and – Erich Kuester Mar 05 '20 at 21:23
1

i have no experience with c++, but i use python, in python you have to do this:

button.override_background_color(Gtk.StateFlags.NORMAL, Gdk.RGBA(1.0 , 0.0 , 0.0, 1.0))

maybe in c++ you have to do this

button.override_background_color(GTK_STATE_FLAGS_NORMAL, Gdk::RGBA(1.0 , 0.0 , 0.0, 1.0));
shehata
  • 531
  • 6
  • 20
  • 1
    hi and thanks for your reply. we found out the problem is all in the theme, the only way is to use a css and load it. I will search for the solution and update the question with the result. – giuspen May 12 '12 at 07:57