如何使用gtkmm在TreeView中使单个项目加粗?

时间:2015-07-06 06:35:05

标签: c++ treeview gtkmm

在执行CellRenderer时可以提供Gtk::CellRendererTextappend_column),在其中我可以使用property_weight来设置权重。

然而,这会改变整个专栏,我的目标是能够使单个项目变为粗体。是否有内置的方法来执行此操作?

要求是能够随时将项目从常规文本更改为粗体。

所以,如果我有:

  Gtk::TreeModel::Row row = *(treeModel->append());
  row[treeColumns.id] = 1;
  row[treeColumns.name] = "[one]";

  Gtk::TreeModel::Row childrow = *(treeModel->append(row.children()));
  childrow[treeColumns.id] = 11;
  childrow[treeColumns.name] = "<b>eleven</b>";

  childrow = *(treeModel->append(row.children()));
  childrow[treeColumns.id] = 12;
  childrow[treeColumns.name] = "twelve";

  row = *(treeModel->append());
  row[treeColumns.id] = 2;
  row[treeColumns.name] = "two";

  //Add the TreeView's view columns:
  treeView->append_column("Name", treeColumns.name);
//  treeView->append_column("ID", treeColumns.id);

    treeView->expand_all();

然后我希望元素编号11变为粗体(它目前只有HTML标记以突出显示该点),我将如何做到这一点?

1 个答案:

答案 0 :(得分:2)

如果您指定自己的CellRenderer,那么您可能还想通过set_cell_data_func()指定您自己的cell_data_func: https://developer.gnome.org/gtkmm/stable/classGtk_1_1TreeViewColumn.html#a765296845c9e9757ba405becbbc13d96

在该回调中,您可以更改单元格的属性。它看起来像这样:

MyTreeView::MyTreeView()
{
  ...
  Gtk::TreeViewColumn view_column = Gtk::manage(new Gtk::TreeViewColumn(_("Name")));
  Gtk::CellRendererText cell = Gtk::manage(new Gtk::CellRendererText());

  view_column->pack_start(*cell, false);
  view_column->set_cell_data_func(*cell, sigc::mem_fun(*this, &MyTreeView::on_cell_data_name));
  append_column(view_column);
  ...
}


void MyTreeView::on_cell_data_name(
  Gtk::CellRenderer* renderer,
  const Gtk::TreeModel::iterator& iter)
{
  //Get the value from the model and show it appropriately in the view:
  Gtk::TreeModel::Row row = *iter;
  int id = row[m_columns.m_col_id];
  Glib::ustring name = row[m_columns.m_col_name];

  if (id == SOME_MAGIC_NUMBER)
    name = "<b>" + name + "</b">;

  Gtk::CellRendererText* text_renderer = dynamic_cast<Gtk::CellRendererText*>(renderer);
  if(text_renderer)
    text_renderer.property_markup() = name;
  }
}

您可能更喜欢使用单元格渲染器的字体属性,而不是使用标记。