为什么在gtkmm中使用CellRenderer时TreeView中的信息显示两次
Why is the information displayed twice in TreeView when using CellRenderer in gtkmm
具有以下代码:
MyTreeView::MyTreeView(GtkTreeView* cobject, const Glib::RefPtr<Gtk::Builder>& builder)
: Gtk::TreeView(cobject)
{
Glib::RefPtr< Gtk::TreeStore > treeModel;
treeModel = Gtk::TreeStore::create(treeColumns);
this->set_model(treeModel);
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] = "eleven";
childrow = *(treeModel->append(row.children()));
childrow[treeColumns.id] = 12;
childrow[treeColumns.name] = "twelve";
row = *(treeModel->append());
row[treeColumns.id] = 2;
row[treeColumns.name] = "two";
row = *(treeModel->append());
row[treeColumns.id] = 3;
row[treeColumns.name] = "three";
childrow = *(treeModel->append(row.children()));
childrow[treeColumns.id] = 31;
childrow[treeColumns.name] = "thirtyone";
Gtk::TreeModel::Row subchild = *(treeModel->append(childrow.children()));
subchild[treeColumns.id] = 311;
subchild[treeColumns.name] = "three hundred eleven";
Gtk::TreeViewColumn *view_column = Gtk::manage(new Gtk::TreeViewColumn("Name", treeColumns.name));
Gtk::CellRendererText *cellRenderer = Gtk::manage(new Gtk::CellRendererText());
view_column->pack_start(*cellRenderer, false);
view_column->set_cell_data_func(*cellRenderer, sigc::mem_fun(*this, &MyTreeView::on_cell_data_name));
this->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[treeColumns.id];
Glib::ustring name = row[treeColumns.name];
Gtk::CellRendererText* text_renderer = dynamic_cast<Gtk::CellRendererText*>(renderer);
if (id == 11)
{
text_renderer->property_weight().set_value(Pango::Weight::WEIGHT_HEAVY);
}
else
{
text_renderer->property_weight().set_value(Pango::Weight::WEIGHT_NORMAL);
}
if(text_renderer)
{
text_renderer->property_text() = name;
}
}
我有以下内容:
所以数据在某种子列或类似的列中是重复的。为什么会这样?我该怎么做才能只有呈现的文本(使用正确的字体)。
我在没有使用Glade的情况下做了个例子,问题依旧:
treeview.cpp:
#include <gtkmm.h>
#include "MyWindow.hpp"
int main(int argc, char *argv[])
{
Glib::RefPtr<Gtk::Application> app = Gtk::Application::create(argc, argv, "org.spike.treeview");
MyWindow window;
app->run(window);
return 0;
}
MyWindow.hpp:
#include <gtkmm.h>
#include "MyTreeView.hpp"
class MyWindow : public Gtk::ApplicationWindow
{
public:
MyWindow();
virtual ~MyWindow();
protected:
MyTreeView *treeView;
};
MyTreeView.hpp:
#include <gtkmm.h>
class MyTreeView : public Gtk::TreeView
{
public:
MyTreeView();
void on_cell_data_name(Gtk::CellRenderer* renderer, const Gtk::TreeModel::iterator& iter);
class ModelColumns : public Gtk::TreeModel::ColumnRecord
{
public:
ModelColumns()
{ add(id); add(name); add(type); add(publicKey); add(privateKey); }
Gtk::TreeModelColumn<int> id;
Gtk::TreeModelColumn<Glib::ustring> name;
Gtk::TreeModelColumn<char> type;
Gtk::TreeModelColumn<Glib::ustring> publicKey;
Gtk::TreeModelColumn<Glib::ustring> privateKey;
} treeColumns;
};
MyWindow.cpp:
#include <iostream>
#include "MyWindow.hpp"
MyWindow::MyWindow()
{
treeView = Gtk::manage(new MyTreeView());
add(*treeView);
show_all_children();
}
MyWindow::~MyWindow()
{}
MyTreeView.cpp:
#include <iostream>
#include "MyTreeView.hpp"
MyTreeView::MyTreeView()
{
Glib::RefPtr< Gtk::TreeStore > treeModel;
treeModel = Gtk::TreeStore::create(treeColumns);
this->set_model(treeModel);
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] = "eleven";
childrow = *(treeModel->append(row.children()));
childrow[treeColumns.id] = 12;
childrow[treeColumns.name] = "twelve";
row = *(treeModel->append());
row[treeColumns.id] = 2;
row[treeColumns.name] = "two";
row = *(treeModel->append());
row[treeColumns.id] = 3;
row[treeColumns.name] = "three";
childrow = *(treeModel->append(row.children()));
childrow[treeColumns.id] = 31;
childrow[treeColumns.name] = "thirtyone";
Gtk::TreeModel::Row subchild = *(treeModel->append(childrow.children()));
subchild[treeColumns.id] = 311;
subchild[treeColumns.name] = "three hundred eleven";
// this->append_column_editable("Name (orig)", treeColumns.name);
// this->append_column("Id", treeColumns.id);
Gtk::TreeViewColumn *view_column = Gtk::manage(new Gtk::TreeViewColumn("Name", treeColumns.name));
Gtk::CellRendererText *cellRenderer = Gtk::manage(new Gtk::CellRendererText());
view_column->pack_start(*cellRenderer, false);
view_column->set_cell_data_func(*cellRenderer, sigc::mem_fun(*this, &MyTreeView::on_cell_data_name));
this->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[treeColumns.id];
Glib::ustring name = row[treeColumns.name];
Gtk::CellRendererText* text_renderer = dynamic_cast<Gtk::CellRendererText*>(renderer);
if (id == 11)
{
text_renderer->property_weight().set_value(Pango::Weight::WEIGHT_HEAVY);
}
else
{
text_renderer->property_weight().set_value(Pango::Weight::WEIGHT_NORMAL);
}
if(text_renderer)
{
text_renderer->property_text() = name;
}
}
因为这一行:
Gtk::TreeViewColumn *view_column = Gtk::manage(new Gtk::TreeViewColumn("Name", treeColumns.name));
您指定了 TreeModel 列而不仅仅是标题,因此它添加了一个默认单元格渲染器,该渲染器使用该列中该数据的默认渲染。你需要这个:
Gtk::TreeViewColumn *view_column = Gtk::manage(new Gtk::TreeViewColumn("Name"));
具有以下代码:
MyTreeView::MyTreeView(GtkTreeView* cobject, const Glib::RefPtr<Gtk::Builder>& builder)
: Gtk::TreeView(cobject)
{
Glib::RefPtr< Gtk::TreeStore > treeModel;
treeModel = Gtk::TreeStore::create(treeColumns);
this->set_model(treeModel);
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] = "eleven";
childrow = *(treeModel->append(row.children()));
childrow[treeColumns.id] = 12;
childrow[treeColumns.name] = "twelve";
row = *(treeModel->append());
row[treeColumns.id] = 2;
row[treeColumns.name] = "two";
row = *(treeModel->append());
row[treeColumns.id] = 3;
row[treeColumns.name] = "three";
childrow = *(treeModel->append(row.children()));
childrow[treeColumns.id] = 31;
childrow[treeColumns.name] = "thirtyone";
Gtk::TreeModel::Row subchild = *(treeModel->append(childrow.children()));
subchild[treeColumns.id] = 311;
subchild[treeColumns.name] = "three hundred eleven";
Gtk::TreeViewColumn *view_column = Gtk::manage(new Gtk::TreeViewColumn("Name", treeColumns.name));
Gtk::CellRendererText *cellRenderer = Gtk::manage(new Gtk::CellRendererText());
view_column->pack_start(*cellRenderer, false);
view_column->set_cell_data_func(*cellRenderer, sigc::mem_fun(*this, &MyTreeView::on_cell_data_name));
this->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[treeColumns.id];
Glib::ustring name = row[treeColumns.name];
Gtk::CellRendererText* text_renderer = dynamic_cast<Gtk::CellRendererText*>(renderer);
if (id == 11)
{
text_renderer->property_weight().set_value(Pango::Weight::WEIGHT_HEAVY);
}
else
{
text_renderer->property_weight().set_value(Pango::Weight::WEIGHT_NORMAL);
}
if(text_renderer)
{
text_renderer->property_text() = name;
}
}
我有以下内容:
所以数据在某种子列或类似的列中是重复的。为什么会这样?我该怎么做才能只有呈现的文本(使用正确的字体)。
我在没有使用Glade的情况下做了个例子,问题依旧:
treeview.cpp:
#include <gtkmm.h>
#include "MyWindow.hpp"
int main(int argc, char *argv[])
{
Glib::RefPtr<Gtk::Application> app = Gtk::Application::create(argc, argv, "org.spike.treeview");
MyWindow window;
app->run(window);
return 0;
}
MyWindow.hpp:
#include <gtkmm.h>
#include "MyTreeView.hpp"
class MyWindow : public Gtk::ApplicationWindow
{
public:
MyWindow();
virtual ~MyWindow();
protected:
MyTreeView *treeView;
};
MyTreeView.hpp:
#include <gtkmm.h>
class MyTreeView : public Gtk::TreeView
{
public:
MyTreeView();
void on_cell_data_name(Gtk::CellRenderer* renderer, const Gtk::TreeModel::iterator& iter);
class ModelColumns : public Gtk::TreeModel::ColumnRecord
{
public:
ModelColumns()
{ add(id); add(name); add(type); add(publicKey); add(privateKey); }
Gtk::TreeModelColumn<int> id;
Gtk::TreeModelColumn<Glib::ustring> name;
Gtk::TreeModelColumn<char> type;
Gtk::TreeModelColumn<Glib::ustring> publicKey;
Gtk::TreeModelColumn<Glib::ustring> privateKey;
} treeColumns;
};
MyWindow.cpp:
#include <iostream>
#include "MyWindow.hpp"
MyWindow::MyWindow()
{
treeView = Gtk::manage(new MyTreeView());
add(*treeView);
show_all_children();
}
MyWindow::~MyWindow()
{}
MyTreeView.cpp:
#include <iostream>
#include "MyTreeView.hpp"
MyTreeView::MyTreeView()
{
Glib::RefPtr< Gtk::TreeStore > treeModel;
treeModel = Gtk::TreeStore::create(treeColumns);
this->set_model(treeModel);
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] = "eleven";
childrow = *(treeModel->append(row.children()));
childrow[treeColumns.id] = 12;
childrow[treeColumns.name] = "twelve";
row = *(treeModel->append());
row[treeColumns.id] = 2;
row[treeColumns.name] = "two";
row = *(treeModel->append());
row[treeColumns.id] = 3;
row[treeColumns.name] = "three";
childrow = *(treeModel->append(row.children()));
childrow[treeColumns.id] = 31;
childrow[treeColumns.name] = "thirtyone";
Gtk::TreeModel::Row subchild = *(treeModel->append(childrow.children()));
subchild[treeColumns.id] = 311;
subchild[treeColumns.name] = "three hundred eleven";
// this->append_column_editable("Name (orig)", treeColumns.name);
// this->append_column("Id", treeColumns.id);
Gtk::TreeViewColumn *view_column = Gtk::manage(new Gtk::TreeViewColumn("Name", treeColumns.name));
Gtk::CellRendererText *cellRenderer = Gtk::manage(new Gtk::CellRendererText());
view_column->pack_start(*cellRenderer, false);
view_column->set_cell_data_func(*cellRenderer, sigc::mem_fun(*this, &MyTreeView::on_cell_data_name));
this->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[treeColumns.id];
Glib::ustring name = row[treeColumns.name];
Gtk::CellRendererText* text_renderer = dynamic_cast<Gtk::CellRendererText*>(renderer);
if (id == 11)
{
text_renderer->property_weight().set_value(Pango::Weight::WEIGHT_HEAVY);
}
else
{
text_renderer->property_weight().set_value(Pango::Weight::WEIGHT_NORMAL);
}
if(text_renderer)
{
text_renderer->property_text() = name;
}
}
因为这一行:
Gtk::TreeViewColumn *view_column = Gtk::manage(new Gtk::TreeViewColumn("Name", treeColumns.name));
您指定了 TreeModel 列而不仅仅是标题,因此它添加了一个默认单元格渲染器,该渲染器使用该列中该数据的默认渲染。你需要这个:
Gtk::TreeViewColumn *view_column = Gtk::manage(new Gtk::TreeViewColumn("Name"));