2014-09-01 35 views
0

我有一個簡單的樹視圖與基於文本的單元格。我希望能夠通過按F2鍵打開當前選中的單元格進行編輯。換句話說,當選擇一個單元格時,F2鍵的行爲應與Enter鍵現在的行爲完全相同。 (它打開一個小盒子,我可以在其中編輯單元格的內容。)如何激活樹視圖中的單元格進行編輯

我找不出要調用哪個以激活該小盒子。

我有一個最低工作例如:

#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); 
} 

回答

1

我找到了答案。 (關鍵是標題爲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); 
} 
0

您可以切換編輯/不可編輯與下面的代碼F2鍵。

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

     // What code should go here to make the currently selected cell active for editing? 
     Gtk::CellRendererText *column_fruit = (Gtk::CellRendererText*)m_tree_view.get_column_cell_renderer(0); 
     Gtk::CellRendererText *column_vegetable = (Gtk::CellRendererText*)m_tree_view.get_column_cell_renderer(1); 
     column_fruit->property_editable() = !column_fruit->property_editable(); 
     column_vegetable->property_editable() = !column_vegetable->property_editable(); 
    } 
} 
+0

不完全是我所需要的。我不需要它是*可編輯*,但處於活動編輯狀態(就像我按Enter鍵一樣)。 – AthanasiusOfAlex 2014-09-02 11:25:59

+0

對不起,我沒有簡單的想法。要獲得焦點列很困難,需要調整指針的座標。 – Wataru 2014-09-02 13:35:38

+1

另一個資源是https://en.wikibooks.org/wiki/GTK%2B_By_Example/Tree_View/Editable_Cells。 – AthanasiusOfAlex 2014-09-06 08:35:20

相關問題