如何使用 gtkmm 在 TreeView 中使单个项目加粗?
How to make a single item bold in TreeView using gtkmm?
可以在执行 append_column
时提供 CellRenderer
(Gtk::CellRendererText
),我可以在其中用户 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 标签只是为了突出这一点),我该怎么做?
如果您指定自己的 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;
}
}
不使用标记,您可能更喜欢使用单元格渲染器的字体 属性。
可以在执行 append_column
时提供 CellRenderer
(Gtk::CellRendererText
),我可以在其中用户 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 标签只是为了突出这一点),我该怎么做?
如果您指定自己的 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;
}
}
不使用标记,您可能更喜欢使用单元格渲染器的字体 属性。