【问题标题】:Combobox in Toolbar工具栏中的组合框
【发布时间】:2013-10-28 07:52:32
【问题描述】:

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

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

这里是 Toolbar+Combobox 的一个例子:

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;
}

【问题讨论】:

    标签: combobox gtk toolbar vala


    【解决方案1】:

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

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

    【讨论】:

      猜你喜欢
      • 2012-01-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-11-06
      • 1970-01-01
      • 2012-04-19
      相关资源
      最近更新 更多