2016-12-06 98 views
2

我正在尝试学习vala。使用我的示例应用程序,我遇到了GLib.Menu操作的问题。GLib.Menu操作不起作用

我宣布了一个新的操作quit_action应该退出应用程序。编译器运行时没有任何警告或错误,但是当我运行应用程序时,我可以打开菜单,但项目“退出”呈灰色。

/* main.vala */ 

using Gtk; 

class mainWindow : Gtk.ApplicationWindow { 
     public mainWindow (Application app) { 

      // Window Properties 
      this.set_default_size (800, 600); 
      this.window_position = Gtk.WindowPosition.CENTER; 
     this.destroy.connect (Gtk.main_quit); 

      // HeaderBar 
      var headerBar = new Gtk.HeaderBar(); 
      headerBar.set_title ("Test Title"); 
      headerBar.set_subtitle ("Test Subtitle"); 
      headerBar.set_show_close_button (true); 

      // Menu 
      var menuButton = new Gtk.Button.from_icon_name ("open-menu-symbolic", Gtk.IconSize.SMALL_TOOLBAR); 
      headerBar.pack_end (menuButton); 

      var menu = new GLib.Menu(); 
     menu.append ("Quit", "app.quit"); 

      var popover = new Gtk.Popover (menuButton); 
      popover.bind_model (menu, "app"); 

      menuButton.clicked.connect (() => { 
       popover.popup(); 
       popover.show_all(); 
      }); 

      this.set_titlebar (headerBar); 

      this.show_all(); 
     } 
} 

public class Application : Gtk.Application { 
    public Application() { 
     Object (application_id: "org.example.application", flags: 0); 
    } 

    protected override void activate() { 
     // Create a new window for this application. 
     mainWindow win = new mainWindow (this); 
     win.show_all(); 
     Gtk.main(); 
    } 

    protected override void startup() { 
     base.startup(); 

     var quit_action = new GLib.SimpleAction ("quit", null); 
     quit_action.activate.connect (this.quit); 
     this.add_action (quit_action); 
    } 
} 

int main (string[] args) { 
    Gtk.init (ref args); 
    return new Application().run (args); 
} 

回答

1

有两件事情错在这里:

  1. 你必须分配给ApplicationWindow应用程序(这是在构造函数正常完成,但因为它是能说会道的风格构造你不能调用继承的构造):

    this.application = app; 
    
  2. 的动作名称必须匹配,如果你想使用app.quit它被称为在这两个地方的方式。

全工作代码:

/* main.vala */ 

using Gtk; 

class mainWindow : Gtk.ApplicationWindow { 
    public mainWindow (Application app) { 
    this.application = app; 
    // Window Properties 
    this.set_default_size (800, 600); 
    this.window_position = Gtk.WindowPosition.CENTER; 
    this.destroy.connect (Gtk.main_quit); 

    // HeaderBar 
    var headerBar = new Gtk.HeaderBar(); 
    headerBar.set_title ("Test Title"); 
    headerBar.set_subtitle ("Test Subtitle"); 
    headerBar.set_show_close_button (true); 

    // Menu 
    var menuButton = new Gtk.Button.from_icon_name ("open-menu-symbolic", Gtk.IconSize.SMALL_TOOLBAR); 
    headerBar.pack_end (menuButton); 

    var menu = new GLib.Menu(); 
    menu.append ("Quit", "app.quit"); 

    var popover = new Gtk.Popover (menuButton); 
    popover.bind_model (menu, "app"); 

    menuButton.clicked.connect (() => { 
     //popover.popdown(); 
     popover.show_all(); 
    }); 

    this.set_titlebar (headerBar); 

    this.show_all(); 
    } 
} 

public class Application : Gtk.Application { 
    public Application() { 
    Object (application_id: "org.example.application", flags: 0); 
    } 

    protected override void activate() { 
    // Create a new window for this application. 
    mainWindow win = new mainWindow (this); 
    win.show_all(); 
    Gtk.main(); 
    } 

    protected override void startup() { 
    base.startup(); 

    var quit_action = new GLib.SimpleAction ("app.quit", null); 
    quit_action.activate.connect (this.quit); 
    this.add_action (quit_action); 
    } 
} 

int main (string[] args) { 
    Gtk.init (ref args); 
    return new Application().run (args); 
}