How to use a ComboBox with a liststore? Filling liststore doesn't show itens in combobox

Hello, good day,
How to use a ComboBox with a liststore? Example below doesn’t fill any item in the combo. Tried the examples in gtk-rs github, but there is only one example about liststore, thats very hard to understand, and nothing about a combobox.

        let store = gtk::ListStore::new(&[String::static_type()]);

        for _ in 0..10 {
            store.set(&store.append(), &[(0, &"I'm a child item")]);
            //store.insert_with_values( Some(i), &[(0, &"I'm a item of the list")]);
        }

        label_combo.set_model(Some(&store));

image

Hej,

I’d suggest you should just use gtk::StringList like so:

let store = gtk::StringList::new();
store.append(test);
label_combo.set_model(Some(&store));

Also keep in mind that DropDown is a more modern alternative to the usual ComoBox, you may want to give that a go.

Hope that helps

1 Like

Hello. Thanks for your help. I’ll just try and post the results. Thank you.

Hello, I think this objects are not avaiable in gtk3. I forgot to post that I’m working on gtk-rs 3
https://gtk-rs.org/gtk3-rs/stable/latest/docs/gtk/?search=DropDown

Ah, if you’re still on GTK3 you probably want to use gtk::ComboBoxText. Otherwise you’ll have to set a CellRenderer which renders the text of yours. See the documentation of ComboBox for that:

The GtkComboBox uses the model-view pattern; the list of valid choices is specified in the form of a tree model, and the display of the choices can be adapted to the data in the model by using cell renderers, as you would in a tree view. This is possible since GtkComboBox implements theGtkCellLayout interface. The tree model holding the valid choices is not restricted to a flat list, it can be a real tree, and the popup will reflect the tree structure.

(Gtk – 3.0)

The following program works for me:

use gtk::prelude::*;

fn build_ui(application: &gtk::Application) {
    let window = gtk::ApplicationWindow::new(application);

    window.set_title("Example");
    window.set_border_width(10);
    window.set_position(gtk::WindowPosition::Center);
    window.set_default_size(350, 70);

    let combobox = gtk::ComboBoxText::new();
    let store = gtk::ListStore::new(&[String::static_type()]);

    for _ in 0..10 {
        store.set(&store.append(), &[(0, &"Test")]);
    }

    combobox.set_model(Some(&store));

    window.add(&combobox);

    window.show_all();
}

fn main() {
    let application =
        gtk::Application::new(Some("com.github.gtk-rs.examples.basic"), Default::default());

    application.connect_activate(build_ui);

    application.run();
}

image

2 Likes

Hello @Cogitri, good morning,

It really works. Thank you. I’m doing a project in github to help documentation with this demo application.

I already added your example in it
image

. Do you know something about creating custom stores that I can put on combo, with icons or widgets?

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.