mirror of
https://github.com/RGBCube/serenity
synced 2025-05-22 22:05:07 +00:00
LibGUI: ComboBox, add "set_selected_index" method
Easy way to set selected item.
This commit is contained in:
parent
db692a807b
commit
7194b4823e
2 changed files with 13 additions and 1 deletions
|
@ -67,6 +67,7 @@ ComboBox::ComboBox()
|
||||||
ASSERT(model());
|
ASSERT(model());
|
||||||
auto new_value = model()->data(index).to_string();
|
auto new_value = model()->data(index).to_string();
|
||||||
m_editor->set_text(new_value);
|
m_editor->set_text(new_value);
|
||||||
|
if (!m_only_allow_values_from_model)
|
||||||
m_editor->select_all();
|
m_editor->select_all();
|
||||||
close();
|
close();
|
||||||
deferred_invoke([this, index](auto&) {
|
deferred_invoke([this, index](auto&) {
|
||||||
|
@ -94,6 +95,15 @@ void ComboBox::set_model(NonnullRefPtr<Model> model)
|
||||||
m_list_view->set_model(move(model));
|
m_list_view->set_model(move(model));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void ComboBox::set_selected_index(size_t index)
|
||||||
|
{
|
||||||
|
auto model = this->m_list_view->model();
|
||||||
|
|
||||||
|
auto model_index = model->index(index, 0);
|
||||||
|
if (model->is_valid(model_index))
|
||||||
|
this->m_list_view->selection().set(model_index);
|
||||||
|
}
|
||||||
|
|
||||||
void ComboBox::select_all()
|
void ComboBox::select_all()
|
||||||
{
|
{
|
||||||
m_editor->select_all();
|
m_editor->select_all();
|
||||||
|
|
|
@ -46,6 +46,8 @@ public:
|
||||||
const Model* model() const;
|
const Model* model() const;
|
||||||
void set_model(NonnullRefPtr<Model>);
|
void set_model(NonnullRefPtr<Model>);
|
||||||
|
|
||||||
|
void set_selected_index(size_t index);
|
||||||
|
|
||||||
bool only_allow_values_from_model() const { return m_only_allow_values_from_model; }
|
bool only_allow_values_from_model() const { return m_only_allow_values_from_model; }
|
||||||
void set_only_allow_values_from_model(bool);
|
void set_only_allow_values_from_model(bool);
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue