首页 > 解决方案 > Gtk.Stack 不会更改事件回调函数中的可见子项

问题描述

所以我目前正在开发一个基本操作系统的应用程序并遇到了一个问题。我有一个窗口,左侧有一个 Granite.Sourcelistview,右侧有一个包含不同视图的堆栈。我的问题是,当按下其中一个屏幕(我创建的项目设置屏幕)上的按钮时,堆栈应该将当前视图更改为另一个视图,但事实并非如此。当前视图保持不变。

这是窗口:

public class MainWindow : Gtk.Window {

private SourceListStackView srcl_view {get; set;}

construct {

    var header = new Gtk.HeaderBar ();
    header.show_close_button = true;

    //this is the source list view
    srcl_view = new SourceListStackView ();

    var paned = new Gtk.Paned (Gtk.Orientation.HORIZONTAL);
    paned.position = 130;
    paned.pack1 (srcl_view, false, false);
    paned.add2 (srcl_view.stack);

    add(paned);

    set_titlebar (header);
}

public static int main(string[] args) {
    Gtk.init (ref args);

    MainWindow app = new MainWindow ();
    app.show_all ();
    Gtk.main ();
    return 0;
}
}

这是我创建的 sourcelistview 类:

    public class SourceListStackView : Granite.Widgets.SourceList {

    public Gtk.Stack stack {get; set;}

public SourceListStackView () {
    var project_page = new ProjectSettings ();
    stack = new Gtk.Stack ();

    var project = new Granite.Widgets.SourceList.ExpandableItem("Root");

    this.root.add(project);

    stack.add_named(project_page, "hello");

    //here depending on what item of the sourcelist is created,
    //the view with the same name as the item 
    //should be displayed (not the best mechanism but works)

    this.item_selected.connect ((item) => {
        if(item != null){
            stack.visible_child_name = item.name;
        }  
    });

    //problematic part is here: This won't change the current view..why? 
    //The button should add another item to the
    // source list view and change the current view
    // to the newly created Welcome Screen but it doesn't do that..
    project_page.button.clicked.connect(() => {
        project.add(new Granite.Widgets.SourceList.Item ("Welcome"));
        stack.add_named(new Granite.Widgets.Welcome("bla bli blu", "bla"), "Welcome");
        stack.set_visible_child_name("Welcome"); 
    });

}
}

这是带有应该触发视图更改的按钮的视图:

public class ProjectSettings : Granite.SimpleSettingsPage {
public Gtk.Button button {get; set;}
public ProjectSettings () {
    Object (
        activatable: false,
        description: "This is a screen",
        header: "",
        icon_name: "preferences-system",
        title: "Screen"
    );
}

construct {
    var project_name_label = new Gtk.Label ("Name");
    project_name_label.xalign = 1;

    var project_name_entry = new Gtk.Entry ();
    project_name_entry.hexpand = true;
    project_name_entry.placeholder_text = "Peter";

    content_area.attach (project_name_label, 0, 0, 1, 1);
    content_area.attach (project_name_entry, 1, 0, 1, 1);

    button = new Gtk.Button.with_label ("Save Settings");

    action_area.add (button);
}
}

不起作用的部分是这个:

//problematic part is here: This won't change the current view.. why?
project_page.button.clicked.connect(() => {
    project.add(new Granite.Widgets.SourceList.Item ("Welcome"));
    stack.add_named(new Granite.Widgets.Welcome("bla bli blu", "bla"), "Welcome");
    stack.set_visible_child_name("Welcome");
});

我不知道为什么它不会改变视图。我特别告诉它将可见的孩子设置为“欢迎”(这正是我在上面一行中命名它的方式),但它不会出现。有人可以向我解释为什么吗?我可以轻松地在信号/事件之外更改堆栈的可见子项,但在它内部不会起作用..

非常感谢

更新:问题通过下面 José Fonte 的评论得到解决:我实例化了视图,在其上调用了show_all ()方法,然后将其添加到堆栈中并为其设置可见子项。

标签: gtkgtk3vala

解决方案


推荐阅读