1

私は一緒に仕事をしようGMenuとしています。composite templates

gtk-mumble.vala

using GLib;
using Gtk;

namespace GtkMumble {

[GtkTemplate (ui = "/net/antiochus/gtk-mumble/gtk_mumble.ui")]
public class MainWindow : Gtk.ApplicationWindow {
    public MainWindow (Gtk.Application app) {
        Object (application: app, title: "gtk-mumble");
        var about_action = new SimpleAction ("about", null);
        about_action.activate.connect (this.about_cb);
        this.add_action (about_action);
        this.show ();
    }

    void about_cb (SimpleAction simple, Variant? parameter) {
        print ("This does nothing.  It is only a demonstration.\n");
    }

    [GtkCallback]
    public void on_destroy () 
    {
        application.quit ();
    }
}

public class Application : Gtk.Application {

    public Application () {
        Object (application_id: "net.antiochus.gtk-mumble");
    }

    protected override void activate () {
        assert(this is Gtk.Application);
        new MainWindow (this);
    }

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

        var menu = new GLib.Menu ();
        menu.append ("About", "win.about");
        menu.append ("Quit", "app.quit");
        this.app_menu = menu;

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

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

}

gtk_mumble.ui

<?xml version="1.0" encoding="UTF-8"?>
<!-- Generated with glade 3.18.0 -->
<interface>
  <requires lib="gtk+" version="3.0"/>
  <template class="GtkMumbleMainWindow" parent="GtkApplicationWindow">
    <property name="visible">True</property>
    <property name="can_focus">False</property>
    <property name="title" translatable="yes">gtk-mumble</property>
    <property name="default_width">499</property>
    <property name="default_height">399</property>
    <signal name="destroy" handler="on_destroy" swapped="no"/>
    <child>
      <object class="GtkButton" id="connect_button">
        <property name="label" translatable="yes">Connect</property>
        <property name="visible">True</property>
        <property name="can_focus">True</property>
        <property name="receives_default">True</property>
        <signal name="clicked" handler="on_clicked" swapped="no"/>
      </object>
    </child>
  </template>
</interface>

リソース.xml

<?xml version="1.0" encoding="UTF-8"?>
<gresources>
  <gresource prefix="/net/antiochus/gtk-mumble">
    <file compressed="true" preprocess="xml-stripblanks">gtk_mumble.ui</file>
  </gresource>
</gresources>

ビルド コマンド:

glib-compile-resources resources.xml --target=resources.c --sourcedir=. --c-name gtk_mumble --generate-source
valac -o gtk-mumble gtk-mumble.vala resources.c --target-glib=2.40 --pkg gtk+-3.0 --pkg gee-0.8 --gresources resources.xml

このコードにより、実行時にアサーションが失敗します。

(gtk_mumble:19708): Gtk-CRITICAL **: gtk_application_get_menubar: assertion 'GTK_IS_APPLICATION (application)' failed

GMenu機能しませんが、残りの UI は機能します (たとえば、UI テンプレートが読み込まれ、ボタンがあり、信号が機能します)。

[Gtk..]すべての属性を削除するGMenuと機能し、アサーションは失敗しませんが、もちろん残りの UI は機能しなくなります。

編集:回避策として、Gtk.Boxテンプレートを使用して作成し、このボックスをメイン ウィンドウに追加します。それは機能しますが、直接的なアプローチが機能しない理由を知りたいです。

私が使用しているバージョンは次のとおりです。

  • Vala 0.22.1 (0.23.3でも試しました)
  • GLib 2.40.0
  • Gtk+ 3.12.0
4

3 に答える 3

1

多くの試行錯誤の後、私は問題を見つけました。

ui ファイルには、ウィンドウを可視に設定するディレクティブがあります。

<property name="visible">True</property>

そのディレクティブを削除し、代わりにshow ();ウィンドウ クラスのコンストラクターに明示的に追加すると、アサーションは起動しなくなり、GMenu はテンプレートで動作します :)。

于 2014-04-09T17:55:31.910 に答える
0

アプリケーションを存続させてみてください:

var app = new Application ();
return app.run (args);
于 2014-03-27T12:09:53.300 に答える