如何使用 gtk_label_set_attributes 更改 GtkLabel 的文本大小?

How do I change the text size of a GtkLabel using gtk_label_set_attributes?

我有这个:

GtkWidget *const Label = gtk_label_new("Welcome");
gtk_label_set_attributes((GtkLabel *)Label, ({
    PangoAttrList *const Attrs = pango_attr_list_new();
    pango_attr_list_insert(Attrs, pango_attr_size_new(36));
    Attrs;
}));
gtk_container_add((GtkContainer *)Window, Label);

我试图在不使用某种需要解析的标记语言的情况下更改 GtkLabel 的文本大小

当前结果:

没有显示文字

预期结果:

文本显示为已使用 gtk_label_set_markup("<span font=\"36\">Welcome</span>");

我该如何进行?

更新:

以下作品显示文字

GtkWidget *const Label = gtk_label_new(GameDirStr+2);
PangoAttrList *const Attrs = pango_attr_list_new();
PangoAttribute *const SizeAttr = pango_attr_size_new(72*PANGO_SCALE);
pango_attr_list_insert(Attrs, SizeAttr);
// pango_attribute_destroy(SizeAttr); // SEGFAULT
gtk_label_set_attributes((GtkLabel *)Label, Attrs);
pango_attr_list_unref(Attrs); // WORKS
pango_attribute_destroy(SizeAttr); // SEGFAULT even after the attributes were added to the label
gtk_container_add((GtkContainer *)Window, Label);

BUT documentation 声明我最终应该使用 pango_attribute_destroy() 释放它们,但这样做会导致段错误,即使在我将它们添加到列表后也是如此

如何正确地释放它们?

使用 Pango 属性更改文本大小的过程还包括释放内存等预期做法

我认为属性应该是 pango 字体描述符。您可以设置大小、系列等,然后将其添加到属性列表中。见以下代码:

attrlist = pango_attr_list_new();

PangoFontDescription * font_desc = pango_font_description_new();
pango_font_description_set_size(font_desc, 36 * PANGO_SCALE);
PangoAttribute * attr = pango_attr_font_desc_new(font_desc);

pango_attr_list_insert(attrlist, attr);

gtk_label_set_attributes((GtkLabel *) Label, attrlist);

有一个大问题:

在将属性添加到标签后销毁它们。尝试将属性设置为 NULL 的标签将导致分段错误。

所以,简而言之,创建属性,创建列表,将属性添加到列表,将属性列表设置为标签,使用属性 destroy 删除属性,使用 pango list unref 删除列表。

关于使用pango_attribute_destroy(),我发现答案是不要这样做,因为pango_attr_list_unref()会自动释放列表中包含的任何属性

Decrease the reference count of the given attribute list by one. If the result is zero, free the attribute list and the attributes it contains.

BUT the documentation states that I should eventually free them using pango_attribute_destroy(), but doing so causes a segfault, even after I added them to the list

How do I properly deallocate them?

你不必。属性列表拥有传递给 pango_attr_list_insert() 的属性 请参阅 documentation (PangoAttribute *attr is [transfer full]). So you no longer owns the PangoAttribute unless you add additional references. Also, see related gi documentation