揭示和隐藏搜索条目 - Gtk

时间:2016-04-23 07:13:42

标签: gtk vala genie

背景

这是练习的一部分,我在精灵中创建一个小文本编辑器。到目前为止,该应用程序运行良好。

目标

我在设置一个搜索输入框时遇到困难,该搜索输入框在点击标题栏的搜索按钮后显示。

代码

init
    Gtk.init (ref args)

    var app = new Application ()
    app.show_all ()
    Gtk.main ()

// This class holds all the elements from the GUI
class Application : Gtk.Window

    _view:Gtk.TextView
    _filename:string
    _search_button:Gtk.Button
    _search_entry:Gtk.SearchEntry
    _search_entry_revealer:Gtk.Revealer
    _search_button_revealer:Gtk.Revealer
    _search_visible:bool = false

    construct ()

        // Prepare Gtk.Window:
        this.window_position = Gtk.WindowPosition.CENTER
        this.destroy.connect (Gtk.main_quit)
        this.set_default_size (400, 400)


        // Headerbar definition
        headerbar:Gtk.HeaderBar = new Gtk.HeaderBar()
        headerbar.show_close_button = true
        headerbar.set_title("My text editor")

        // Headerbar buttons
        open_button:Gtk.ToolButton = new ToolButton.from_stock(Stock.OPEN)
        open_button.clicked.connect (openfile)

        new_button:Gtk.ToolButton = new ToolButton.from_stock(Stock.NEW)
        new_button.clicked.connect (createNew)

        save_button:Gtk.ToolButton = new ToolButton.from_stock(Stock.SAVE)
        save_button.clicked.connect (saveFile)

        var search_box = new Gtk.Box (Gtk.Orientation.HORIZONTAL, 0);
        _search_entry:Gtk.SearchEntry = new Gtk.SearchEntry();
        _search_entry.editable = true;
        _search_entry.visibility = true;
        _search_entry.expand = true;
        _search_entry.max_width_chars = 30;
        _search_entry.margin_right = 12;

        _search_entry_revealer:Gtk.Revealer = new Gtk.Revealer();
        _search_button_revealer:Gtk.Revealer = new Gtk.Revealer();
        _search_entry_revealer.transition_type = Gtk.RevealerTransitionType.SLIDE_LEFT;
        _search_button_revealer.transition_type = Gtk.RevealerTransitionType.SLIDE_LEFT;

        _search_button:Gtk.Button = new Gtk.Button.from_icon_name ("edit-find-symbolic", Gtk.IconSize.LARGE_TOOLBAR)
        _search_button.has_tooltip = true
        _search_button.tooltip_text = "Search your current notebook"
        _search_button.clicked.connect(show_search)

        _search_button_revealer.add(_search_button)
        _search_entry_revealer.add(_search_entry)
        _search_entry_revealer.reveal_child = false
        _search_button_revealer.reveal_child = true

        // Add everything to the toolbar
        headerbar.pack_start (save_button)
        headerbar.pack_start (open_button)
        headerbar.pack_start (new_button)
        search_box.add (_search_button_revealer)
        search_box.add (_search_entry_revealer)
        headerbar.pack_end (search_box)

        show_all ()
        this.set_titlebar(headerbar)

        // Box:
        box:Gtk.Box = new Gtk.Box (Gtk.Orientation.VERTICAL, 1)
        this.add (box)

        // A ScrolledWindow:
        scrolled:Gtk.ScrolledWindow = new Gtk.ScrolledWindow (null, null)
        box.pack_start (scrolled, true, true, 0)

        // The TextView:
        _view = new Gtk.TextView ()
        _view.set_wrap_mode (Gtk.WrapMode.WORD)
        _view.buffer.text = "Lorem Ipsum"
        scrolled.add (_view)


    def show_search()
        _search_button_revealer.reveal_child = false
        _search_entry_revealer.reveal_child = true
        show_all()
        _search_visible = true
        _search_entry.can_focus = true
        _search_entry.grab_focus()

问题

我的方法是创建一个小函数(show_search)以实现搜索条目的显示/隐藏。程序编译正常,但我在执行时遇到错误:

(text_editor-exercise_7_1:9732): Gtk-CRITICAL **: gtk_revealer_set_reveal_child: assertion 'GTK_IS_REVEALER (revealer)' failed

(text_editor-exercise_7_1:9732): Gtk-CRITICAL **: gtk_revealer_set_reveal_child: assertion 'GTK_IS_REVEALER (revealer)' failed

(text_editor-exercise_7_1:9732): GLib-GObject-CRITICAL **: g_object_set: assertion 'G_IS_OBJECT (object)' failed

(text_editor-exercise_7_1:9732): Gtk-CRITICAL **: gtk_widget_grab_focus: assertion 'GTK_IS_WIDGET (widget)' failed

单击搜索按钮时会发生这些情况。有人能指出我在这段代码中的错误吗?这个错误抱怨什么? GTK_IS_WIDGET断言失败了吗?

1 个答案:

答案 0 :(得分:2)

我提取了代码中有问题的部分:

class Application : Gtk.Window

    _search_entry_revealer:Gtk.Revealer
    _search_button_revealer:Gtk.Revealer

    construct ()
        _search_entry_revealer:Gtk.Revealer = new Gtk.Revealer();
        _search_button_revealer:Gtk.Revealer = new Gtk.Revealer();

    def show_search()
        _search_button_revealer.reveal_child = false
        _search_entry_revealer.reveal_child = true

您在类中声明了Gtk.Revealer类型的两个属性。

然后在构造方法中声明两个类型为Gtk.Revealer的局部变量。

因此,属性永远不会设置为任何值。

然后,对属性的访问会产生您正在获得的错误。

解决方案很简单,你必须将类型留在构造方法中的赋值中,以便声明局部变量,而是使用属性(如你所希望的那样):

class Application : Gtk.Window

    _search_entry_revealer:Gtk.Revealer
    _search_button_revealer:Gtk.Revealer

    construct ()
        // No type annotation here, so no local variables are declared
        _search_entry_revealer = new Gtk.Revealer();
        _search_button_revealer = new Gtk.Revealer();

    def show_search()
        _search_button_revealer.reveal_child = false
        _search_entry_revealer.reveal_child = true