Gtk.Stack不会在事件回调函数

时间:2018-05-01 16:56:04

标签: gtk gtk3 vala

所以我目前正在为基本操作系统开发应用程序并遇到问题。 我有一个窗口,左边是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()方法,然后将其添加到堆栈并将可见子项设置为它。

0 个答案:

没有答案