How to use CssStyleProvider in Gtkmm3

会有一股神秘感。 提交于 2019-12-05 21:52:26

问题


I'm programming in c++ with gtkmm3. I want to change some fonts in my program. I read all the things about CssStyleProvider, StyleContext in gtkmm documentation and also in gtk+ documentaion but i couldn't make it work and couldn't find any tutorial about that. I'm trying like that

Glib::ustring data;
data="GtkMenuBar, GtkMenuItem {font-name: Sans 6}"; /*tried with semi-colon, too*/
Glib::RefPtr<Gtk::CssProvider> asd = Gtk::CssProvider::create();
Glib::RefPtr<Gtk::StyleContext> asd2 = Gtk::StyleContext::create();
asd->load_from_data(data);
asd2->add_provider(asd, 0); /*also tried different priorities*/

also tried like that

Glib::ustring data;
data="GtkMenuBar, GtkMenuItem {font-name: Sans 6}"; /*tried with semi-colon, too*/
Glib::RefPtr<Gtk::CssProvider> asd = Gtk::CssProvider::create();
Glib::RefPtr<Gdk::Screen> screen;
Glib::RefPtr<Gtk::StyleContext> asd2 = Gtk::StyleContext::create();
asd->load_from_data(data);
(mainWindow.get())->get_property("screen", screen);
asd2->add_provider(screen, asd, 0); /*also tried different priorities*/

And I tried these before running my main window and after running it but i have no luck. Any help is appreciated.


回答1:


Have you also to tried to use the macro GTK_STYLE_PROVIDER_PRIORITY_APPLICATION instead of 0?

asd2->add_provider(screen, asd, macro GTK_STYLE_PROVIDER_PRIORITY_APPLICATION);

Works for me, also on Windows.

Example:

int main(int argc, char **argv) {

  Main app(argc, argv);
  Glib::ustring data = "GtkLabel {color: #ff00ea;font: Comic Sans MS 12}";
  auto css = CssProvider::create();
  if(not css->load_from_data(data)) {
      cerr << "Failed to load css\n";
      std::exit(1);
  }
  Label label("test");
  Window win;
  auto screen = Gdk::Screen::get_default();
  auto ctx = label.get_style_context();
  ctx->add_provider_for_screen(screen, css, GTK_STYLE_PROVIDER_PRIORITY_APPLICATION);
  win.add(label);
  win.show_all();

  app.run(win);
  return 0;
}



回答2:


I don't know about gtkmm and css, but css on the web requires a ; after font-name:Sans 6

Other than that, everything you have looks right based on the GTK::CssProvider reference



来源:https://stackoverflow.com/questions/6666387/how-to-use-cssstyleprovider-in-gtkmm3

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!