如何激活树状图中的单元格进行编辑
How to activate a cell in tree view for editing
我有一个简单的树视图,其中包含基于文本的单元格。我希望能够通过按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);
}
#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);
}
您可以通过下面的代码用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();
}
}
相关文章:
- 将QIcon添加到QTableView单元格
- 当用户在qtablewidget中输入单元格时,如何获得信号?C++
- 查找矩阵中单元格的相邻元素
- C++:在多个线程中访问同一数组/向量的不同单元格是否会产生数据竞赛?
- 当我使用需要计算数组单元格地址的模板时,奇怪的C++行为
- PyQT - 在 QTableWidget 单元格中显示垂直文本
- 2D 数组,某些单元格保存特定数据
- 为什么通过定义另一个指针单元格,整个代码停止工作?
- CLI/C++dataGridView示例-设置comobox单元格列表的索引
- 用单元格表示迷宫
- 为什么不能使用 setData() 在 QTreeView 中设置单元格的背景颜色?
- 将 GtkTreeView 中的单元格设置为以编程方式编辑模式
- 如何防止Qt中的当前单元格更改信号或跳过"processing"?
- 将当前多边形数据单元与下一个多边形单元格 - VTK 进行比较
- 在qtableView中编辑单元格之后,如何避免使用选项卡键进行编辑模式
- 如何在listcontrolmfc中编辑单元格
- wxGrid 在用户编辑单元格后显示旧值而不是新值
- 如何使用C++从 Excel 文档中写入、编辑和检索特定单元格
- MFC CListCtrl 在单元格上显示文本框以供用户编辑
- 如何激活树状图中的单元格进行编辑