Get DropDown selected value as string #1813
-
Hello! use gtk::prelude::*;
fn on_activate(application: >k::Application)
{
let window = gtk::ApplicationWindow::new(application);
let button = gtk::Button::with_label("Print");
let drop_down = gtk::DropDown::default();
let strings = vec!["one", "two", "three"];
let string_list = gtk::StringList::new(strings.as_slice());
drop_down.set_model(Some(&string_list));
let combobox = gtk::ComboBoxText::default();
strings.iter().for_each(|c| combobox.append_text(c));
let drop_down_clone = drop_down.clone();
let combobox_clone = combobox.clone();
button.connect_clicked(move |_| {
let selected_value_drop_down = match drop_down_clone.selected_item()
{
Some(obj) => String::from("???"), // <--- obj has type gtk::glib::Object. How to get string value from obj?
None => String::from("None")
};
println!("{}", selected_value_drop_down);
let selected_value_combobox = match combobox_clone.active_text()
{
Some(str) => String::from(str.as_str()),
None => String::from("None")
};
println!("{}", selected_value_combobox);
});
let grid = gtk::Grid::default();
grid.attach(&button, 0, 0, 1, 1);
grid.attach(&drop_down, 0, 1, 1, 1);
grid.attach(&combobox, 0, 2, 1, 1);
window.set_child(Some(&grid));
window.present();
}
fn main()
{
let app = gtk::Application::builder().build();
app.connect_activate(on_activate);
app.run();
} |
Beta Was this translation helpful? Give feedback.
Answered by
bilelmoussaoui
Jul 29, 2024
Replies: 1 comment 1 reply
-
using |
Beta Was this translation helpful? Give feedback.
1 reply
Answer selected by
eauno1re
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
using
obj.downcast::<gtk::StringObject>()