gtk(mm) 3按钮背景颜色改变

发布于 2024-12-28 03:00:11 字数 1115 浏览 2 评论 0原文

我正在尝试将按钮的背景颜色更改为红色,但似乎不起作用。 我粘贴示例代码。 如果有人可以告诉我如何修复我的代码,请帮忙。

#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;
}

更新: 好的,这就是我解决的方法:

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);

filename.css 的内容是:

column-header .button {
    background-image: -gtk-gradient (linear,
        left top,
        left bottom,
        from (#51cccc),
        color-stop (0.5, darker (#51cccc)),
        to (#51cccc));
}

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));
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

孤芳又自赏 2025-01-04 03:00:11

事实上,我经历了惨痛的教训才明白,CssProvider 是处理此类事情的正确方法。你不需要使用glade,最好是在你的代码中包含css,就像这样

#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);

你必须处理background-image,因为background-color与默认background-image重叠,因此不会改变。
我的智慧来自 Ruby-GNOME2

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

尝蛊 2025-01-04 03:00:11

我没有c++经验,但我使用python,在python中你必须这样做:

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

也许在c++中你必须这样做

button.override_background_color(GTK_STATE_FLAGS_NORMAL, Gdk::RGBA(1.0 , 0.0 , 0.0, 1.0));

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));
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文