0

我正在按照本教程中的步骤在 Gtkd(不是 gtkmm)中创建 TreeModel ComboBox:

https://developer.gnome.org/gtkmm-tutorial/stable/combobox-example-full.html.en

但我真的坚持下去了。

我认为将 ListStore 设置为组合框的一种方法是:

ListStore store = new ListStore([GType.INT, GType.STRING]);

TreeIter iter1 = store.createIter();
store.setValue(iter1, 0, 0);
store.setValue(iter1, 1, "Item 0");

TreeIter iter2 = store.createIter();
store.setValue(iter2, 0, 1);
store.setValue(iter2, 1, "Item 1");

obj_combobox.setModel(store);

CellRendererText renderer  = new CellRendererText();
obj_combobox.packStart(renderer, true);
obj_combobox.addAttribute(renderer, "text", 1);

但是我怎样才能从中获取选定的值呢?

4

1 回答 1

0

这个例子有效:

module gtkd_combobox;
// Compile: ldc -w  main.d  `pkg-config --cflags --libs gtkd-3`

import std.stdio, core.stdc.stdlib;
import gtk.Builder;
import gtk.Main, gtk.Window, gtk.Button, gtk.ComboBox, gtk.Widget;
import gtk.TreeIter, gtk.TreeModel, gtk.TreeModelIF;
import gtk.ListStore, gtk.CellRendererText;

void on_click_btn_test(ComboBox *cbbox){
    TreeIter iter;
    TreeModelIF model;
    string item;

    cbbox.getActiveIter(iter);

    model = cbbox.getModel();
    item = model.getValueString(iter, 2); // Get value from column 2 (extra data)
    writeln(item);
}

void main(string[] args) {
    Main.init(args);

    Builder g = new Builder();
    g.addFromFile("vista.glade");

    // Widgets from glade file "vista.glade":
    Window w = cast(Window)g.getObject("window1");
    w.setDefaultSize(350, 200);
    w.setTitle("Gtkd Combobox example");
    Button btn_test = cast(Button)g.getObject("btn_test");
    ComboBox cbo = cast(ComboBox)g.getObject("cbo_test");

    // Actions:
    w.addOnHide( delegate void(Widget aux){ Main.quit(); } );
    btn_test.addOnClicked(delegate void(Button aux){ on_click_btn_test(&cbo); } );

    // Combo Items:
    ListStore store;

    store = new ListStore([GType.INT, GType.STRING, GType.STRING]);

    TreeIter iter1 = store.createIter();
    TreeIter iter2 = store.createIter();

    store.setValue(iter1, 0, 0); // iter, column=0, value=0
    store.setValue(iter1, 1, "Item 1"); // iter, column=1, value="Item 1"
    store.setValue(iter1, 2, "Extra data 1"); // iter, column=2, value="Extra data 1"

    store.setValue(iter2, 0, 1);
    store.setValue(iter2, 1, "Item 2");
    store.setValue(iter2, 2, "Extra data 2");

    cbo.setModel(store);
    CellRendererText col  = new CellRendererText();
    cbo.packStart(col, true);
    cbo.addAttribute(col, "text", 1);

    cbo.setActive(0);

    w.showAll();
    Main.run();
}
于 2016-09-06T20:35:56.523 回答