0

我正在尝试在 aGtkLabel的右侧添加 aGtkHeaderBar然后更改字体粗细,但我无法做到。

我有这样的 XML:

<?xml version="1.0" encoding="UTF-8"?>
<interface>
  <template class="ExampleAppWindow" parent="GtkApplicationWindow">
    <property name="title" translatable="yes">Demo</property>
    <property name="default-width">800</property>
    <property name="default-height">600</property>
    <child type="titlebar">
      <object class="GtkHeaderBar" id="header">
        <child type="start">
          <object class="GtkLabel" id="version">
            <property name="label" translatable="yes">Version 1.0.0</property>
          </object>
        </child>
      </object>
    </child>
    <child>
      <object class="GtkBox" id="content_box">
        <property name="orientation">horizontal</property>
        <child>
          <object class="GtkLabel" id="demo">
            <property name="label">Work in progress</property>
          </object>
        </child>
      </object>
    </child>
  </template>
</interface>

然后我有代码

#include <gtk/gtk.h>

#include "example.h"
#include "exampleappwin.h"

struct _ExampleAppWindow
{
  GtkApplicationWindow parent;
};

G_DEFINE_TYPE(ExamplAppWindow, example_app_window, GTK_TYPE_APPLICATION_WINDOW);

static void
example_app_window_init (ExampleAppWindow *win)
{
    GtkCssProvider *cssProvider = gtk_css_provider_new ();
    gtk_css_provider_load_from_resource (cssProvider, "/org/gtk/exampleapp/theme.css");

    gtk_style_context_add_provider (gtk_widget_get_style_context(GTK_WIDGET (win)),
                               GTK_STYLE_PROVIDER(cssProvider),
                               GTK_STYLE_PROVIDER_PRIORITY_USER);
                               
    gtk_widget_init_template (GTK_WIDGET (win));
}

static void
example_app_window_class_init (ExampleAppWindowClass *class)
{
    gtk_widget_class_set_template_from_resource (GTK_WIDGET_CLASS (class),
                                               "/org/gtk/exampleapp/window.ui");
}

ExampleAppWindow *
example_app_window_new (ExampleApp *app)
{
  return g_object_new (EXAMPLE_APP_WINDOW_TYPE, "application", app, NULL);
}

然后我就有了这样的风格

GtkLabel {
    color: red;
    font-weight: bold;
}

最后是资源

<?xml version="1.0" encoding="UTF-8"?>
<gresources>
  <gresource prefix="/org/gtk/exampleapp">
    <file preprocess="xml-stripblanks">window.ui</file>
    <file>theme.css</file>
  </gresource>
</gresources>

一切都是用介子编译的

example_resources = gnome.compile_resources('asistent_resources',
  'example.gresource.xml',
  source_dir: '.')

但它什么也没做。标签不会改变其样式。我也试过

<object class="GtkLabel">
  <property name="label" translatable="yes">Version 1.0.0</property>
  <attributes>
    <attribute name="weight" value="PANGO_WEIGHT_BOLD"/>
    <attribute name="background" value="red" start="5" end="10"/>
  </attributes>
</object>

甚至<property name="label" translatable="yes"><b>Version 1.0.0</b></property>,但是当我做这些事情时,标签会从标题中完全消失。

我只能使用更改全局样式

* {
    color: red;
    font-weight: bold;
}

这有效,但使用.classnameor #idorname选择器什么也不做。

我的代码基于这个例子

4

0 回答 0