条目(Entry)
- 8.2.1. 简易用法
- 8.2.2. 条目补全(Entry Completion)
- 8.2.3. 条目图标
- 8.2.4. 条目进度
8.2.1. 简易用法
条目部件允许用户输入文本。你可以使用set_text()方法更改部件当前的内容,还可以使用get_text()方法读取当前的内容。
有时你可能想使Entry部件变为只读的。你可以通过向set_editable()方法传递false做到这点。
对于不想在屏幕上回显的密码、密语或者其他信息,你可以通过向set_visibility()方法传递false使输入文本被隐藏。
当用户在条目部件上输入文本时你可能想要收到通知。为此Gtk::Entry提供了两个信号,他们分别是activate和changed。当用户在条目部件上按下Enter键时,部件将发出activate信号;当部件上的文本发生更改的时候,部件将会发出changed信号。你可以利用这些信号对用户的输入进行校验或者是过滤。将键盘焦点移动到其他的部件可能表示用户已经完成了文本输入。这种时候Gtk::Entry将会发出继承自Gtk::Widget的focus_out_event信号提醒你。带有条目的组合框小结包含了使用这些信号的示例程序。
如果你将true传递给set_activates_default()方法,那么当你在Gtk::Entry中按下Enter的时候包含这个Gtk::Entry的窗口将会激活其默认部件。这在对话框中特别的有用。默认部件通常会是对话框的按钮之一,例如对话框的关闭按钮。若你想主动的将某个部件设置为默认部件,请使用Gtk::Widget::set_can_default()和Gtk::Widget::grab_default()。
- 8.2.1.1. 简单的条目示例
8.2.1.1. 简单的条目示例
本示例使用Gtk::Entry。他还有两个CheckButton,你可以使用他们切换条目部件的可见性和可编辑性标志。
File: examplewindow.h (For use with gtkmm 4)
#ifndef GTKMM_EXAMPLEWINDOW_H #define GTKMM_EXAMPLEWINDOW_H #include <gtkmm.h> class ExampleWindow : public Gtk::Window { public: ExampleWindow(); virtual ~ExampleWindow(); protected: //Signal handlers: void on_checkbox_editable_toggled(); void on_checkbox_visibility_toggled(); void on_button_close(); //Child widgets: Gtk::Box m_HBox; Gtk::Box m_VBox; Gtk::Entry m_Entry; Gtk::Button m_Button_Close; Gtk::CheckButton m_CheckButton_Editable, m_CheckButton_Visible; }; #endif //GTKMM_EXAMPLEWINDOW_H
File: main.cc (For use with gtkmm 4)
#include "examplewindow.h" #include <gtkmm/application.h> int main(int argc, char *argv[]) { auto app = Gtk::Application::create("org.gtkmm.example"); //Shows the window and returns when it is closed. return app->make_window_and_run<ExampleWindow>(argc, argv); }
File: examplewindow.cc (For use with gtkmm 4)
#include "examplewindow.h" #include <iostream> ExampleWindow::ExampleWindow() : m_VBox(Gtk::Orientation::VERTICAL), m_Button_Close("Close"), m_CheckButton_Editable("Editable"), m_CheckButton_Visible("Visible") { set_size_request(200, 100); set_title("Gtk::Entry"); set_child(m_VBox); m_Entry.set_max_length(50); m_Entry.set_text("hello"); m_Entry.set_text(m_Entry.get_text() + " world"); m_Entry.select_region(0, m_Entry.get_text_length()); m_Entry.set_expand(true); m_VBox.append(m_Entry); m_VBox.append(m_HBox); m_HBox.append(m_CheckButton_Editable); m_CheckButton_Editable.set_expand(true); m_CheckButton_Editable.signal_toggled().connect( sigc::mem_fun(*this, &ExampleWindow::on_checkbox_editable_toggled) ); m_CheckButton_Editable.set_active(true); m_HBox.append(m_CheckButton_Visible); m_CheckButton_Visible.set_expand(true); m_CheckButton_Visible.signal_toggled().connect( sigc::mem_fun(*this, &ExampleWindow::on_checkbox_visibility_toggled) ); m_CheckButton_Visible.set_active(true); m_Button_Close.signal_clicked().connect( sigc::mem_fun(*this, &ExampleWindow::on_button_close) ); m_VBox.append(m_Button_Close); m_Button_Close.set_expand(); set_default_widget(m_Button_Close); } ExampleWindow::~ExampleWindow() { } void ExampleWindow::on_checkbox_editable_toggled() { m_Entry.set_editable(m_CheckButton_Editable.get_active()); } void ExampleWindow::on_checkbox_visibility_toggled() { m_Entry.set_visibility(m_CheckButton_Visible.get_active()); } void ExampleWindow::on_button_close() { hide(); }
8.2.2. 条目补全(Entry Completion)
Entry条目组件可以通过用户输入的前几个字符提供预先准备好选项的下拉列表。例如,在搜索对话框中提供以前输入过的文本作为建议。
要启用此功能,你需要创建一个EntryCompletion对象,然后使用Entry部件的set_completion()方法将其提供给条目部件。
EntryCompletion可以用set_model()指定一个包含所有可能的条目的TreeModel。你应该调用set_text_column()指定该模型的那一行用以匹配可能的文本条目。
或是在不方便生成补全条目列表的时候(太大或者生成太麻烦),也可以使用set_match_func()指定一个回调槽。这在你想匹配字符串中开头以外的部分的时候这会很有用。
- 8.2.2.1. 条目补全示例
8.2.2.1. 条目补全示例
这个示例创建一个Gtk::EntryCompletion并将其与Gtk::Entry相关联。补全使用了一个包含了可能条目的Gtk::TreeModel以及一些其他的额外的操作。
File: examplewindow.h (For use with gtkmm 4)
#ifndef GTKMM_EXAMPLEWINDOW_H #define GTKMM_EXAMPLEWINDOW_H #include <gtkmm.h> class ExampleWindow : public Gtk::Window { public: ExampleWindow(); virtual ~ExampleWindow(); protected: //Signal handlers: void on_button_close(); //See the comment in the implementation: //bool on_completion_match(const Glib::ustring& key, const Gtk::TreeModel::const_iterator& iter); //Tree model columns, for the EntryCompletion's filter model: class ModelColumns : public Gtk::TreeModel::ColumnRecord { public: ModelColumns() { add(m_col_id); add(m_col_name); } Gtk::TreeModelColumn<unsigned int> m_col_id; Gtk::TreeModelColumn<Glib::ustring> m_col_name; }; ModelColumns m_Columns; //Child widgets: Gtk::Box m_HBox; Gtk::Box m_VBox; Gtk::Entry m_Entry; Gtk::Label m_Label; Gtk::Button m_Button_Close; }; #endif //GTKMM_EXAMPLEWINDOW_H
File: main.cc (For use with gtkmm 4)
#include "examplewindow.h" #include <gtkmm/application.h> int main(int argc, char *argv[]) { auto app = Gtk::Application::create("org.gtkmm.example"); //Shows the window and returns when it is closed. return app->make_window_and_run<ExampleWindow>(argc, argv); }
File: examplewindow.cc (For use with gtkmm 4)
#include "examplewindow.h" #include <iostream> ExampleWindow::ExampleWindow() : m_VBox(Gtk::Orientation::VERTICAL), m_Label("Press a or b to see a list of possible completions."), m_Button_Close("Close") { //set_size_request(200, 100); set_title("Gtk::EntryCompletion"); set_child(m_VBox); m_VBox.append(m_Entry); m_VBox.append(m_Label); m_Label.set_expand(true); m_Button_Close.signal_clicked().connect( sigc::mem_fun(*this, &ExampleWindow::on_button_close) ); m_VBox.append(m_Button_Close); set_default_widget(m_Button_Close); //Add an EntryCompletion: auto completion = Gtk::EntryCompletion::create(); m_Entry.set_completion(completion); //Create and fill the completion's filter model auto refCompletionModel = Gtk::ListStore::create(m_Columns); completion->set_model(refCompletionModel); // For more complex comparisons, use a filter match callback, like this. // See the comment below for more details: //completion->set_match_func( sigc::mem_fun(*this, //&ExampleWindow::on_completion_match) ); //Fill the TreeView's model auto row = *(refCompletionModel->append()); row[m_Columns.m_col_id] = 1; row[m_Columns.m_col_name] = "Alan Zebedee"; row = *(refCompletionModel->append()); row[m_Columns.m_col_id] = 2; row[m_Columns.m_col_name] = "Adrian Boo"; row = *(refCompletionModel->append()); row[m_Columns.m_col_id] = 3; row[m_Columns.m_col_name] = "Bob McRoberts"; row = *(refCompletionModel->append()); row[m_Columns.m_col_id] = 4; row[m_Columns.m_col_name] = "Bob McBob"; //Tell the completion what model column to use to //- look for a match (when we use the default matching, instead of // set_match_func(). //- display text in the entry when a match is found. completion->set_text_column(m_Columns.m_col_name); } ExampleWindow::~ExampleWindow() { } void ExampleWindow::on_button_close() { hide(); } /* You can do more complex matching with a handler like this. * For instance, you could check for substrings inside the string instead of the start, * or you could look for the key in extra model columns as well as the model column that will be displayed. * The code here is not actually more complex - it's a reimplementation of the default behaviour. * bool ExampleWindow::on_completion_match(const Glib::ustring& key, const Gtk::TreeModel::const_iterator& iter) { if(iter) { const auto row = *iter; const auto key_length = key.size(); auto filter_string = row[m_Columns.m_col_name]; auto filter_string_start = filter_string.substr(0, key_length); //The key is lower-case, even if the user input is not. filter_string_start = filter_string_start.lowercase(); if(key == filter_string_start) return true; //A match was found. } return false; //No match. } */
8.2.3. 条目图标
Entry部件可以在文本区域的开头或结尾显示一个图标。可以使用set_icon_from_pixbuf()或set_icon_from_icon_name()之类的方法指定要显示的图标。应用程序可以通过响应signal_icon_press信号来处理用户按下图标事件。
- 8.2.3.1. 条目图标示例
8.2.3.1. 条目图标示例
这个示例显示带有具名搜索图标的Gtk::Entry部件,并且在按下这个图标的时候将该名称打印到终端上。
File: examplewindow.h (For use with gtkmm 4)
#ifndef GTKMM_EXAMPLEWINDOW_H #define GTKMM_EXAMPLEWINDOW_H #include <gtkmm.h> class ExampleWindow : public Gtk::Window { public: ExampleWindow(); virtual ~ExampleWindow(); protected: //Signal handlers: void on_icon_pressed(Gtk::Entry::IconPosition icon_pos); void on_button_close(); //Child widgets: Gtk::Box m_VBox; Gtk::Entry m_Entry; Gtk::Button m_Button_Close; }; #endif //GTKMM_EXAMPLEWINDOW_H
File: main.cc (For use with gtkmm 4)
#include "examplewindow.h" #include <gtkmm/application.h> int main(int argc, char *argv[]) { auto app = Gtk::Application::create("org.gtkmm.example"); //Shows the window and returns when it is closed. return app->make_window_and_run<ExampleWindow>(argc, argv); }
File: examplewindow.cc (For use with gtkmm 4)
#include "examplewindow.h" #include <iostream> ExampleWindow::ExampleWindow() : m_VBox(Gtk::Orientation::VERTICAL), m_Button_Close("Close") { set_title("Gtk::Entry"); set_child(m_VBox); m_Entry.set_max_length(50); m_Entry.set_text("Hello world"); m_VBox.append(m_Entry); m_Entry.set_icon_from_icon_name("edit-find"); m_Entry.signal_icon_press().connect( sigc::mem_fun(*this, &ExampleWindow::on_icon_pressed) ); m_Button_Close.signal_clicked().connect( sigc::mem_fun(*this, &ExampleWindow::on_button_close) ); m_VBox.append(m_Button_Close); set_default_widget(m_Button_Close); } ExampleWindow::~ExampleWindow() { } void ExampleWindow::on_icon_pressed(Gtk::Entry::IconPosition /* icon_pos */) { std::cout << "Icon pressed." << std::endl; } void ExampleWindow::on_button_close() { hide(); }
8.2.4. 条目进度
Entry部件可以在输入的文本之下显示一个进度条。当部件的set_progress_fraction()或set_progress_pulse_step()方法被调用的时候进度条将显示出来。
- 8.2.4.1. 条目进度示例
8.2.4.1. 条目进度示例
这个示例显示一个带有进度条的Gtk::Entry部件。
File: examplewindow.h (For use with gtkmm 4)
#ifndef GTKMM_EXAMPLEWINDOW_H #define GTKMM_EXAMPLEWINDOW_H #include <gtkmm.h> class ExampleWindow : public Gtk::Window { public: ExampleWindow(); virtual ~ExampleWindow(); protected: //Signal handlers: bool on_timeout(); void on_button_close(); //Child widgets: Gtk::Box m_VBox; Gtk::Entry m_Entry; Gtk::Button m_Button_Close; }; #endif //GTKMM_EXAMPLEWINDOW_H
File: main.cc (For use with gtkmm 4)
#include "examplewindow.h" #include <gtkmm/application.h> int main(int argc, char *argv[]) { auto app = Gtk::Application::create("org.gtkmm.example"); //Shows the window and returns when it is closed. return app->make_window_and_run<ExampleWindow>(argc, argv); }
File: examplewindow.cc (For use with gtkmm 4)
#include "examplewindow.h" #include <iostream> ExampleWindow::ExampleWindow() : m_VBox(Gtk::Orientation::VERTICAL), m_Button_Close("Close") { set_title("Gtk::Entry"); set_child(m_VBox); m_Entry.set_max_length(50); m_Entry.set_text("Hello world"); m_VBox.append(m_Entry); //Change the progress fraction every 0.1 second: Glib::signal_timeout().connect( sigc::mem_fun(*this, &ExampleWindow::on_timeout), 100 ); m_Button_Close.signal_clicked().connect( sigc::mem_fun(*this, &ExampleWindow::on_button_close) ); m_VBox.append(m_Button_Close); set_default_widget(m_Button_Close); } ExampleWindow::~ExampleWindow() { } bool ExampleWindow::on_timeout() { static double fraction = 0; m_Entry.set_progress_fraction(fraction); fraction += 0.01; if(fraction > 1) fraction = 0; return true; } void ExampleWindow::on_button_close() { hide(); }