【问题标题】:Using Pango Markup within a GTK+ TreeView in C在 C 中的 GTK+ TreeView 中使用 Pango 标记
【发布时间】:2018-06-14 14:08:12
【问题描述】:

我有一个完全硬编码的 GtkTreeView,我正在尝试在 Treeview 中的一列上使用 Pango 标记。这需要在该列的文本渲染器上使用单元格数据函数。单元格数据函数的形式为:

// This function uses pango markup on the Gtklabels shown in the TreeView
// A cell data function is a function that is called for a specific cell renderer for each single row before that row is rendered.
static void cell_data_func_label (__attribute__((unused)) GtkTreeViewColumn *column, GtkCellRenderer *renderer, 
            GtkTreeModel *model, GtkTreeIter *iter, __attribute__((unused)) gpointer user_data)
{
  gchar *label;
  gchar *markuptxt;

  // Retrieve the current label
  gtk_tree_model_get (model, iter, CURRENT, &label, -1);

  markuptxt = g_strdup_printf("<i>%s</i>", label);

  g_object_set(renderer, "markup", markuptxt, "text", NULL, NULL);  // markup isn't showing and text field is blank due to "text" == NULL

  g_free(markuptxt);
}

在 GtkWidget *create_view_and_model() 函数中,我通过以下方式设置单元格数据函数:

  // Sets the GtkTreeCellDataFunc to use for the column
  gtk_tree_view_column_set_cell_data_func(column4, text, cell_data_func_label, NULL, NULL);

问题是文本单元格渲染器现在是空白的。我怀疑这可能与在 g_object_set 中的“文本”之后传递 NULL 有关。以下链接:

https://en.wikibooks.org/wiki/GTK%2B_By_Example/Tree_View/Columns_and_Renderers

声明:

使用“标记”属性时,您需要考虑“标记”和“文本”属性似乎并不相互排斥(我想这可以称为错误)。换句话说:每当您设置“标记”(并且之前使用过“文本”属性)时,将“文本”属性设置为 NULL,反之亦然。

可能不再是这种情况(即它适用于 GTK+ 2),但我不确定需要更改哪些内容才能使用标记呈现列,因为除了 NULL 之外的任何内容最终都会被呈现没有标记,使用 NULL 会使渲染器为空。任何帮助将不胜感激。

【问题讨论】:

    标签: c gtk3 pango


    【解决方案1】:

    最终起作用的只是在 g_object_set 中不包括对“文本”的任何引用:

    g_object_set(renderer, "markup", markuptxt, NULL);
    

    【讨论】:

    • 嗨,Leigh,您应该使用其中一个,而不是两个。这个问题取决于哪个优先。如果一个指示标签显示文本和标记,会显示哪一个?这可以用另一个属性来解决,比如布尔值use_markup。我猜如果您交换了设置模型的顺序,首先将文本设置为空,然后将标记作为标记文本,那么单元格将正确显示标记。所以人们会假设设置值的顺序是关键。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-11-02
    • 1970-01-01
    • 2012-11-19
    • 2012-09-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多