c++ - 如何激活 TreeView 中的单元格进行编辑

标签 c++ gtk gtkmm

我有一个带有基于文本的单元格的简单 TreeView 。我希望能够通过按 F2 键打开当前选定的单元格进行编辑。 In other words, the F2 key should behave exactly the same way as the Enter key does now, when a cell is selected. (它会打开一个小框,我可以在其中编辑单元格的内容。)

我不知道要调用什么来激活那个小盒子。

我包括一个最小的工作示例:

#include <gtkmm.h>
#include <iostream>

typedef Glib::RefPtr<Gtk::Application> ApplicationRef;
typedef Glib::RefPtr<Gtk::ListStore>   ListStoreRef;

using namespace std;

class Example : public Gtk::Window
{

public:
    Example()
    {
        m_list_store_ref = Gtk::ListStore::create(m_model_columns);
        m_tree_view.set_model(m_list_store_ref);

        // Fill in the model with dummy data.
        m_add_row("apple", "lettuce");
        m_add_row("orange", "broccoli");
        m_add_row("banana", "cauliflower");

        // Add columns to the tree view.
        m_tree_view.append_column_editable("Fruit", m_model_columns.m_fruit);
        m_tree_view.append_column_editable("Vegetables", m_model_columns.m_vegetables);

        add(m_tree_view);

        add_events(Gdk::KEY_PRESS_MASK);

        show_all_children();
    }
    virtual ~Example() {}

protected:
    //Signal handlers:
    void on_button_clicked() { hide(); }

    //Member widgets:
    Gtk::TreeView m_tree_view;

    // Other objects
    ListStoreRef m_list_store_ref;

    // Model columns
    class ModelColumns : public Gtk::TreeModel::ColumnRecord
    {
    public:
        ModelColumns() { add(m_fruit); add(m_vegetables); }

        Gtk::TreeModelColumn<Glib::ustring> m_fruit;
        Gtk::TreeModelColumn<Glib::ustring> m_vegetables;
    };

    ModelColumns m_model_columns;

    void m_add_row(Glib::ustring fruit, Glib::ustring vegetable)
    {
        auto row = *(m_list_store_ref->append());
        row[m_model_columns.m_fruit]      = fruit;
        row[m_model_columns.m_vegetables] = vegetable;
    }


    bool on_key_press_event(GdkEventKey* event)
    {
        if (event->keyval==GDK_KEY_F2) {
            cout << "F2 was pressed." << endl;

            // What code should go here to make the currently selected cell active for editing?

            return true;
        }

        return Gtk::Window::on_key_press_event(event);
    }
};


int main(int argc, char *argv[])
{
    ApplicationRef app = Gtk::Application::create(argc, argv, "org.gtkmm.examples.mwe");
    Example example;
    return app->run(example);
}

最佳答案

我找到了答案。 (关键是标题为 Start PyGTK cellrenderer edit from code 的帖子。)这是同一个 MWE,它具有我想要的行为:

#include <gtkmm.h>
#include <iostream>

typedef Glib::RefPtr<Gtk::Application> ApplicationRef;
typedef Glib::RefPtr<Gtk::ListStore>   ListStoreRef;

using namespace std;

class Example : public Gtk::Window
{

public:
    Example()
    {
        m_list_store_ref = Gtk::ListStore::create(m_model_columns);
        m_tree_view.set_model(m_list_store_ref);

        // Fill in the model with dummy data.
        m_add_row("apple", "lettuce");
        m_add_row("orange", "broccoli");
        m_add_row("banana", "cauliflower");

        // Add columns to the tree view.
        m_tree_view.append_column_editable("Fruit", m_model_columns.m_fruit);
        m_tree_view.append_column_editable("Vegetables", m_model_columns.m_vegetables);

        add(m_tree_view);

        add_events(Gdk::KEY_PRESS_MASK);

        show_all_children();
    }
    virtual ~Example() {}

protected:
    //Signal handlers:
    void on_button_clicked() { hide(); }

    //Member widgets:
    Gtk::TreeView m_tree_view;

    // Other objects
    ListStoreRef m_list_store_ref;

    // Model columns
    class ModelColumns : public Gtk::TreeModel::ColumnRecord
    {
    public:
        ModelColumns() { add(m_fruit); add(m_vegetables); }

        Gtk::TreeModelColumn<Glib::ustring> m_fruit;
        Gtk::TreeModelColumn<Glib::ustring> m_vegetables;
    };

    ModelColumns m_model_columns;

    void m_add_row(Glib::ustring fruit, Glib::ustring vegetable)
    {
        auto row = *(m_list_store_ref->append());
        row[m_model_columns.m_fruit]      = fruit;
        row[m_model_columns.m_vegetables] = vegetable;
    }


    bool on_key_press_event(GdkEventKey* event)
    {
        if (event->keyval==GDK_KEY_F2) {
            cout << "F2 was pressed." << endl;

            Gtk::TreeModel::Path path;
            Gtk::TreeViewColumn* col;

            m_tree_view.get_cursor(path, col);
            auto cell = col->get_first_cell();

            // If the cell is being edited, cancel the editing;
            // if the cell is not being edited, start editing.
            if(cell->property_editing()) {
                m_tree_view.set_cursor(path, *col, false);
            } else {
                m_tree_view.set_cursor(path, *col, true);
            }

            return true;
        }

        return Gtk::Window::on_key_press_event(event);
    }
};


int main(int argc, char *argv[])
{
    ApplicationRef app = Gtk::Application::create(argc, argv, "org.gtkmm.examples.mwe");
    Example example;
    return app->run(example);
}

关于c++ - 如何激活 TreeView 中的单元格进行编辑,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25608532/

相关文章:

python - 在 GTk python 中禁用文本框的输入

Python - Gtk - 从剪贴板粘贴

c++ - 如何在按钮顶部显示图像小部件

c++ - Gtk+ : How to set the cursor of a window from a Cairo context?

c++ - 通过基类自动进行窗口编程

c++ - pimpl 助手与继承不明确

c++ - 在c++中的 vector 中推回多种类型的数据

c++ - 从线程安全地发出信号并将其连接到小部件

c++ - 在C++中对多维数组进行排序

c++ - memset() 没有按预期工作