首页 > 解决方案 > 如何从 gtkmm 树视图中获取选定的行

问题描述

我有一个使用 ListStore 的 gtkmm TreeView。我将其用作静态列表选择菜单。我在查看用户何时更改行以及选择了哪一行时遇到问题。

我试图使用m_TreeView.signal_row_activated().connect( sigc::mem_fun(*this, &optionList::row_activated) );,但得到的错误

error: no match for call to ‘(sigc::bound_mem_functor0<void, optionList>) (const Gtk::TreePath&, Gtk::TreeViewColumn* const&)’ { return functor_(_A_arg1, _A_arg2); }

我也尝试使用更改的信号,但我收到相同的错误。我在谷歌上发现这些错误并没有什么成果。我还没有看到另一种创建静态列表选择菜单的方法。

我的代码如下:

optionList::optionList() {
    set_policy(Gtk::POLICY_AUTOMATIC, Gtk::POLICY_AUTOMATIC);

    add(m_TreeView);

    m_refListStore = Gtk::ListStore::create(m_Columns);

    m_TreeView.set_model(m_refListStore);
    m_TreeView.set_activate_on_single_click(true);

    std::array<Glib::ustring, 3> options = {"Status", "Plugins", "Config"};

    for(const auto & option : options) {
        std::ostringstream text;
        text << option;

        Gtk::TreeModel::Row row = *(m_refListStore->append());
        row[m_Columns.m_col_text] = text.str();
    }

    m_TreeView.append_column("Options", m_Columns.m_col_text);

    m_TreeView.signal_state_changed().connect( sigc::mem_fun(*this, &optionList::row_activated) ); //This line produces the error


    show_all_children();
}

optionList::row_activated功能只是一个cout,所以我知道它至少被激活了。如果我删除连接线,那么它会编译并运行良好,但我无法判断它是否被选中。

每次选择不同的行时,我都希望A row has been selected在控制台中看到该短语(因为那是输出)。row_activated但是,我什至无法编译包含上述行的代码。

至少,我怎样才能解决这个连接问题,以便代码可以编译和激活功能?

编辑:我相信我搞砸了,需要使用sigc::bind()

标签: c++user-interfacetreeviewselectiongtkmm

解决方案


我能想出的最佳答案是我没有正确通过,为此我需要使用

m_TreeView.signal_state_changed().connect( sigc::bind(sigc::mem_fun(*this, &optionList::row_activated), any parameters) );

推荐阅读