我正在尝试用不同的颜色为TreeView
的行上色。
我在网上找到了以下示例,但是由于某些原因,这些列没有出现,并且窗口保持为空。
我在c++
中使用gtkmm3
。
这是代码:
#include <gtkmm.h>
class Columns : public Gtk::TreeModel::ColumnRecord
{
public:
Columns () { add (name); add (color); add(value); }
Gtk::TreeModelColumn<Glib::ustring> name;
Gtk::TreeModelColumn<int> value;
Gtk::TreeModelColumn<Glib::ustring> color; // a non-display column to store the color for each row
};
class MyWindow : public Gtk::Window
{
public:
MyWindow() :
liststore (Gtk::ListStore::create (cols))
{
// add some random data to the model
Gtk::TreeModel::Row row = *(liststore->append());
row[cols.name] = "foo";
row[cols.value] = 10;
row[cols.color] = "blue";
row = *(liststore->append());
row[cols.name] = "bar";
row[cols.value] = 20;
row[cols.color] = "pink";
row = *(liststore->append());
row[cols.name] = "baz";
row[cols.value] = 30;
row[cols.color] = "green";
treeview = Gtk::manage(new Gtk::TreeView (liststore));
// create a view column for 'name' and associate the 'cell-background'
// property of this column's cellrenderer with the value from the
// 'color' column
Gtk::TreeViewColumn viewcolumn("Name", cols.name);
Gtk::CellRenderer* cellrenderer = viewcolumn.get_first_cell();
viewcolumn.add_attribute(cellrenderer->property_cell_background (), cols.color);
treeview->append_column (viewcolumn);
// do the same for the 'value' column -- cellrenderers are per-
// column, not per-row, so you have to do this for each column's
// cellrenderer if you want to change the color of an entire row
Gtk::TreeViewColumn viewcolumn2("Value", cols.value);
cellrenderer = viewcolumn2.get_first_cell();
viewcolumn2.add_attribute(cellrenderer->property_cell_background (), cols.color);
treeview->append_column (viewcolumn2);
// add it to the window
add (*treeview);
show_all();
}
private:
Columns cols;
Glib::RefPtr<Gtk::ListStore> liststore;
Gtk::TreeView* treeview;
};
int main (int argc, char** argv)
{
auto app = Gtk::Application::create(argc, argv, "org.gtkmm.example");
MyWindow *myWindow = new MyWindow;
return app->run(*myWindow);
}
我想念什么?我使用
get_visible()
进行了检查,并将参数设置为true
。编译和执行时无错误或警告。
编辑:
如果不是创建
treeViewColumn
然后将其添加到TreeView
中,而是直接添加名称和列引用,它将显示该列,但显然我不能以这种方式修改背景色。要明确的是,如果我这样做:
treeview->append_column ("Name", cols.name);
代替 :
Gtk::TreeViewColumn viewcolumn("Name", cols.name);
treeview->append_column (viewcolumn);
最佳答案
这是对我有帮助的解决方案。使用Gtk::manage:
Gtk::TreeView::Column *xxx = Gtk::manage(new Gtk::TreeViewColumn("Name", m_Columns.m_col_name));
m_TreeView.append_column(*xxx)
关于c++ - gtkmm Append_column(TreeViewColumn)不显示任何内容,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/51356670/