工具栏中的组合框

时间:2013-10-28 07:52:32

标签: combobox gtk toolbar vala

抱歉转储(?)问题。如何将GTK Combobox添加到工具栏?我用Google搜索了,但没有找到答案。它编译时没有错误,但是当我运行应用程序时,以下消息将打印到控制台:

Gtk-CRITICAL **: gtk_toolbar_insert: assertion 'GTK_IS_TOOL_ITEM (item)' failed

以下是工具栏+组合框的示例:

using Gtk;

public class Example : Object  {
    private Window _win;
    private Toolbar _tb;

    public Example() {
        _win = new Window();
        _win.title = "Test";
        _win.window_position = WindowPosition.CENTER;
        _win.set_default_size(800, 600);
        _win.destroy.connect(Gtk.main_quit);

        _tb = new Toolbar();
        var img = new Image.from_icon_name("document-new", Gtk.IconSize.SMALL_TOOLBAR);
        var btn = new ToolButton(img, "New");
        _tb.add(btn);

        add_zoombox();

        var vbox = new Box(Orientation.VERTICAL, 0);
        vbox.pack_start(_tb, false, true, 0);

        _win.add(vbox);
    }

    private void add_zoombox() {
        ListStore list = new ListStore(1, typeof (int));
        for(int i = 25; i<= 400; i*=2) {
            TreeIter iter;
            list.append(out iter);
            list.set(iter, 0, i);
        }

        ComboBox cb = new ComboBox.with_model(list);
        CellRendererText r = new CellRendererText();
        cb.pack_start(r, false);
        cb.set_active(0);
        _tb.add(cb);
        cb.show();
    }

    public void show_window() {
        _win.show_all();
    }


}

public static int main (string[] args) {
    Gtk.init(ref args);
    Example ex = new Example();
    ex.show_window();
    Gtk.main();
    return 0;
}

1 个答案:

答案 0 :(得分:2)

我自己解决了这个问题。再次阅读文档后,我发现工具栏只能包含ToolButtons,ToggleToolButtons和RadioToolButtons。要将Combobox或任何其他项添加到工具栏,必须先将其添加到ToolItem。以下是更改的代码:

ToolItem container = new ToolItem();
_tb.add(container);
container.add(cb);
cb.show();