简体   繁体   English

交换gtk文本字段中的背景色(gtkmm C ++)

[英]swapping background colors in gtk text field (gtkmm C++)

Within my GUI (C++, GTKMM 3), i have a text field that is providing some status information. 在我的GUI(C ++,GTKMM 3)中,我有一个提供某些状态信息的文本字段。 i'd like to change the background color of this field (along with the text, which i can easily do), based upon the status. 我想根据状态更改此字段的背景颜色(以及文本,我可以轻松做到)。

there's not a lot out there on how to do this with GTKMM 3.X. 关于如何使用GTKMM 3.X做到这一点,没有很多。 i know i need to use the CssProvider class, and have found some examples on how to load one into the program. 我知道我需要使用CssProvider类,并且找到了一些有关如何将其加载到程序中的示例。 but the examples show how to set the properties one time. 但是示例显示了如何一次设置属性。

but what i haven't figured out is how i can use the CSS properties to change the color of the background, based upon a state (not a state as in 'hover' or anything like that. i want to be able to swap the background from red to green whenever i please). 但是我还没有弄清楚的是我如何使用CSS属性根据状态(不是“悬停”状态或类似的状态)来更改背景颜色。我希望能够交换从红色到绿色的背景,只要我愿意。 if the CSS is written in terms of using the name of the widget, or the type of widget, how do you handle a changing state with the widget to change its properties? 如果CSS是使用小部件的名称或小部件的类型编写的,那么如何使用小部件处理更改状态以更改其属性?

if anyone has any clues, or knows of any examples, i could really use some help. 如果有人有任何线索或知道任何例子,我真的可以使用一些帮助。 the purpose of this is to give the user some immediate feedback at a glance. 这样做的目的是使用户一目了然地立即获得反馈。 in a rush, they won't have to read the status of the box (or from a distance). 匆忙中,他们不必读取包装箱的状态(或从远处读取)。 the color will allow them to gauge what is going on at a glance. 颜色将使他们能够一目了然地了解正在发生的事情。

Adding code 添加代码

this is what i have tried so far (condensed): 这是我到目前为止尝试过的(精简):

std::string style_sheet = ".red_bg {background: #FF0000; color: #000000; } ";
style_sheet += ".green_bg {background: #33FF33; color: #000000; }";
Glib::RefPtr<Gtk::StyleContext> stylecontext = my_text_field->get_style_context();
Glib::RefPtr<Gtk::CssProvider> cssprov = Gtk::CssProvider::create();
cssprov->load_from_data(style_sheet);
stylecontext->add_provider(cssprov, GTK_STYLE_PROVIDER_PRIORITY_USER);
stylecontext->add_class("red_bg");
stylecontext->context_save();

so that works. 这样有效。 when the program fires up, i get a text entry with a red background. 当程序启动时,我得到一个带有红色背景的文本输入。

but later on, if i do the following, nothing happens: 但是后来,如果我执行以下操作,则什么也没有发生:

Glib::RefPtr<Gtk::StyleContext>stylecontext = my_text_field->get_style_context();
stylecontext->remove_class("red_bg");
stylecontext->context_save();  // probably not necessary
stylecontext->add_class("green_bg");
stylecontext->context_save();

at that point, the background stays red. 此时,背景保持红色。 no transition from red to green. 从红色到绿色没有过渡。 i've seen suggestions to use the override_background_color function in the GtkWidget object, but that doesn't work. 我已经看到了在GtkWidget对象中使用override_background_color函数的建议,但这是行不通的。 that only changes the color that is used when you highlight the text in the widget. 仅更改在小部件中突出显示文本时使用的颜色。 i'd still like to see it done the CSS way. 我仍然希望看到它完成CSS方式。

You could do away with the CSS and just use override_background_color , a standard GTK widget method: 您可以删除CSS,而只需使用override_background_color (标准的GTK小部件方法)即可:

override_background_color (StateFlags state, RGBA color)

Sets the background color to use for a widget. 设置要用于小部件的背景颜色。

All other style values are left untouched. 所有其他样式值均保持不变。

Note: This API is mostly meant as a quick way for applications to change a widget appearance. 注意:此API主要是作为应用程序更改小部件外观的快速方法。 If you are developing a widgets library and intend this change to be themeable, it is better done by setting meaningful CSS classes and regions in your widget/container implementation through add_class and add_region . 如果您正在开发小部件库并且希望使此更改具有主题性,那么最好通过add_classadd_region在小部件/容器实现中设置有意义的CSS类和区域来完成。

This way, your widget library can install a CssProvider with the STYLE_PROVIDER_PRIORITY_FALLBACK priority in order to provide a default styling for those widgets that need so, and this theming may fully overridden by the user's theme. 这样一来,你的控件库可以安装一个CssProviderSTYLE_PROVIDER_PRIORITY_FALLBACK为了优先提供的默认样式为那些需要这样的小部件,而这种主题化可以由用户的主题完全覆盖。

Note: Note that for complex widgets this may bring in undesired results (such as uniform background color everywhere), in these cases it is better to fully style such widgets through a CssProvider with the STYLE_PROVIDER_PRIORITY_APPLICATION priority. 注意:请注意,对于复杂的窗口小部件,这可能会带来不想要的结果(例如,各处的背景颜色均相同),在这种情况下,最好通过具有STYLE_PROVIDER_PRIORITY_APPLICATION优先级的CssProvider完全对此类窗口小部件进行样式STYLE_PROVIDER_PRIORITY_APPLICATION

Parameters: 参数:

  • StateFlags state the state for which to set the background color StateFlags state要设置背景色的状态
  • RGBA color the color to assign, or null to undo the effect of previous calls to override_background_color RGBA color为要分配的RGBA color设置颜色,或者为null来撤消先前对override_background_color调用的效果

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM