为啥在gtkmm中使用CellRenderer时TreeView中会显示两次信息

Posted

技术标签:

【中文标题】为啥在gtkmm中使用CellRenderer时TreeView中会显示两次信息【英文标题】:Why is the information displayed twice in TreeView when using CellRenderer in gtkmm为什么在gtkmm中使用CellRenderer时TreeView中会显示两次信息 【发布时间】:2015-07-07 14:34:42 【问题描述】:

有以下代码:

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::Builder (Glade),以简化操作。 @murrayc 我已经做了一个完全没有 Glade 的例子,问题仍然存在 但是我可以看看吗?您可以编辑您的问题。 @murrayc 你还看不出来吗? 【参考方案1】:

因为这条线:

Gtk::TreeViewColumn *view_column = Gtk::manage(new Gtk::TreeViewColumn("Name", treeColumns.name));

您已经指定了 TreeModel 列而不仅仅是标题,因此它添加了一个默认单元格渲染器,该列中的数据具有默认渲染器。你需要这个:

Gtk::TreeViewColumn *view_column = Gtk::manage(new Gtk::TreeViewColumn("Name"));

【讨论】:

非常感谢。我开始慢慢但肯定地弄清楚这件事。

以上是关于为啥在gtkmm中使用CellRenderer时TreeView中会显示两次信息的主要内容,如果未能解决你的问题,请参考以下文章

为啥我的 JTable CellRenderer 一直在运行?

为啥 gtkmm 有时会自动创建第二个线程?

为啥 gtkmm 行 get_value 不起作用?

信号从 gtkmm 弹出列表中触发两次

为啥我的 Linux 编译的二进制文件在 Windows 上运行时不起作用?

当列具有返回 React 组件的 cellRenderer 时,AgGridReact rowData 更改时出错