如何在 Vala 中制作带有菜单栏的 Gtk4 应用程序?

问题描述 投票:0回答:1

我尝试制作一个简单的 Gtk4 应用程序,并尝试添加一个菜单栏,其中的菜单可以执行两件事:打印“Hello,World”并退出。

代码按预期使用命令进行编译

valac --pkg=gtk4 menutest.vala

但是,虽然出现了窗口,但菜单栏却没有出现。我已经在 macOS(使用 Homebrew)和 Fedora 中尝试过此操作。我做错了什么?

这是示例代码:

// menutest.vala

using Gtk;

public class Menutest : Gtk.Application {

    public Menutest() {
        Object (application_id: "org.gtk.menutest");
    }

    protected override void activate() {
        var window = new ApplicationWindow (this);
        window.title = "Hello World";
     
        // Create a menu with two items
        var menu = new Menu();
        menu.insert(0, "Hello", "app.hello");
        menu.insert(1, "Quit", "app.quit");

        // Set the menu as the application's menubar
        this.set_menubar(menu);

        // Define the actions for the menu items
        SimpleAction hello = new SimpleAction("hello", null);
        hello.activate.connect(hello_action);
        this.add_action(hello);

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

        window.present();
    }

    public static int main (string[] args) {
        return new Menutest().run (args);
    }

    // The callback function for the "Hello" action
    private void hello_action (SimpleAction action, Variant? parameter) {
        print ("Hello, world!\n");
    }

    // The callback function for the "Quit" action
    private void quit_action (SimpleAction action, Variant? parameter) {
        this.quit ();
    }
}

macOS 中的结果(屏幕顶部也没有添加菜单):

Fedora 中的结果:

menu menubar vala gtk4
1个回答
0
投票

问题是您缺少菜单栏的子菜单项。添加子菜单后,代码现在可以按预期工作:

using Gtk;

public class Menutest : Gtk.Application {

    public Menutest() {
        Object (application_id: "org.gtk.menutest");
    }

    protected override void activate() {
        var window = new ApplicationWindow (this);
        window.title = "Hello World";
     
        // Create root menu (menu bar).
        var menu = new Menu ();
        
        // Create window submenu and insert (The "Window" entry you'll see in the menu bar)
        var window_menu = new Menu ();
        window_menu.insert(0, "Hello", "app.hello");
        window_menu.insert(1, "Quit", "app.quit");

        // Add window submenu to menu bar
        menu.append_submenu ("Window", window_menu);
        
        // Set the menu as the application's menubar
        this.set_menubar(menu);
        window.set_show_menubar (true);

        // Define the actions for the menu items
        SimpleAction hello = new SimpleAction("hello", null);
        hello.activate.connect(hello_action);
        this.add_action(hello);

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

        window.present();
    }

    public static int main (string[] args) {
        return new Menutest().run (args);
    }

    // The callback function for the "Hello" action
    private void hello_action (SimpleAction action, Variant? parameter) {
        print ("Hello, world!\n");
    }

    // The callback function for the "Quit" action
    private void quit_action (SimpleAction action, Variant? parameter) {
        this.quit ();
    }
}

© www.soinside.com 2019 - 2024. All rights reserved.