gtk - 瓦拉 Gtk 模板 : UI Resource not found

标签 gtk vala ninja meson-build

我正在尝试使用 GtkTemplate,但它确实不起作用。我已经定义了 .ui 文件资源,正确调用它等等。

这是我的文件:

介子.build:

# project name and programming language
project('myproject', 'vala', 'c', version: '1.0.0')

# add resources to the executeable
gnome = import('gnome')
gresources = gnome.compile_resources(
    meson.project_name() + '.resources',
    'data/gresources.xml',
    c_name: 'resources'
)

executable(
    meson.project_name(),

    'src/OpenFileWindow.vala',
    'src/Main.vala',

    gresources,

    dependencies: [
        dependency('gtk+-3.0'),
        dependency('gio-2.0'),
    ],
    install: true
)

src/OpenFileWindow.vala:

using Gtk;

namespace MyProject {

    [GtkTemplate (ui="/ui/OpenFileWindow.ui")]
    public class OpenFileWindow : Window {
        [GtkChild]
        Button btn_browse;

        public OpenFileWindow() {

        }

        [GtkCallback]
        private void btn_browse_clicked(Button btn) {
            stdout.printf("CLICKED");
        }
    }
}

ui/OpenFileWindow.ui:

<?xml version="1.0" encoding="UTF-8"?>
<!-- Generated with glade 3.22.1 -->
<interface>
  <requires lib="gtk+" version="3.20"/>
  <template class="OpenFileWindow" parent="GtkWindow">
    <property name="can_focus">False</property>
    <child>
      <object class="GtkButton" id="btn_browse">
        <property name="label">gtk-open</property>
        <property name="visible">True</property>
        <property name="can_focus">True</property>
        <property name="receives_default">True</property>
        <property name="use_stock">True</property>
        <property name="image_position">top</property>
        <property name="always_show_image">True</property>
        <signal name="clicked" handler="myproject_openfilewindow_btn_browse_clicked" swapped="no"/>
      </object>
    </child>
  </template>
</interface>

数据/gresources.xml:

<?xml version="1.0" encoding="UTF-8"?>
<gresources>
  <gresource prefix="/">
    <file preprocess="xml-stripblanks">ui/OpenFileWindow.ui</file>
  </gresource>
</gresources>

当我用介子和忍者构建时,它给出了这个错误:

valac -C --debug --debug --pkg gio-2.0 --pkg gtk+-3.0 --color=always --directory myproject@exe --basedir ../ --gresources=../data/gresources.xml ../src/OpenFileWindow.vala ../src/Main.vala

../src/OpenFileWindow.vala:6.5-6.40: error: UI resource not found: `/ui/OpenFileWindow.ui'. Please make sure to specify the proper GResources xml files with --gresources and alternative search locations with --gresourcesdir.
    public class OpenFileWindow : Window {

这是什么问题,我实在看不出来...谢谢!

最佳答案

GResource是一个只读文件系统,用于已嵌入到已编译的二进制文件中的文件。在 Vala GUI 项目中,这可用于在二进制文件中存储图像、图标等。

GtkBuilder UI 定义文件也可以嵌入,Vala 通过 [GtkTemplate][GtkChild][GtkCallback] 属性对此提供了额外支持。这种支持的一部分包括编译时的类型检查。检查需要获取源文件并计算出 GResource 内存文件系统的文件名,这就是项目失败的地方。任何想要改进这一点的人都应该修补 codegen/valagtkmodule.vala在 Vala 编译器中。然而,目前,为了让您的项目正常运行,您需要对资源使用更扁平的文件结构。

首先将data/gresource.xml移动到ui/目录。然后将前缀更改为/ui。这意味着内存文件系统将使用与您在 Vala 代码中使用的 GtkTemplate 属性匹配的名称。还要从文件名中删除 ui/ 以提供平面目录结构:

<?xml version="1.0" encoding="UTF-8"?>
<gresources>
  <gresource prefix="/ui/">
    <file preprocess="xml-stripblanks">OpenFileWindow.ui</file>
  </gresource>
</gresources>

您还需要修改meson.build文件中gresources的定义:

gresources = gnome.compile_resources(
    meson.project_name() + '.resources',
    'ui/gresources.xml',
    source_dir: ['ui']
)

这使用 source_dir 来保持引用在扁平目录结构中工作。

您现在应该不再收到错误消息。看看Geary作为使用大量 GtkBuilder UI 文件的示例项目。该项目的 GResource 文件与其他文件位于同一目录中。

您的项目仍然无法编译,因为 Vala 编译器已识别出 [GtkCallback] 没有信号。这是一个名称解析问题,您只需将 OpenFileWindow.ui 文件中的一行更改为:

<signal name="clicked" handler="myproject_openfilewindow_btn_browse_clicked" swapped="no"/>

<signal name="clicked" handler="btn_browse_clicked" swapped="no"/>

更新

为了使 Vala 编译器对 GtkBuilder UI 定义文件进行类型检查,gresource.xml 只需位于公共(public)祖先目录中。这使得这样的资源目录结构成为可能:

resources/
├── components/
│   └── zoom-bar.ui
├── icons/
│   └── project.svg
├── layouts/
│   └── main-window.ui
├── themes/
│   ├── dark.css
│   └── light.css
└── filelist.gresource.xml

filelist.gresource.xml 文件需要 前缀 /,但子目录从源文件复制到内存中的文件:

<?xml version="1.0" encoding="UTF-8"?>
<gresources>
<gresource prefix="/">
<file preprocess="xml-stripblanks" compressed="true">layouts/main-window.ui</file>
<file preprocess="xml-stripblanks" compressed="true">components/zoom-bar.ui</file>
</gresource>
</gresources>

然后可以在 Vala 中通过以下方式访问它:

[GtkTemplate (ui="/layouts/main-window.ui")]

仍然需要设置 meson.buildgnome.compile_resources()source_dir 参数:

gresources = gnome.compile_resources(
    'project-resources',
    'resources/filelist.gresource.xml',
    source_dir: ['resources']
)

关于gtk - 瓦拉 Gtk 模板 : UI Resource not found,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60987956/

相关文章:

python - 在 Fedora 24 上的 Python3 中使用 `cairo.Region` 时出现段错误

vala - 控制 Vapi 文件中的 ref 类型

带有 NEEDS_TERMINAL 的 Glib AppInfo.create_from_commandline 仅启动 xterm 或 gnome-terminal

linux - 使用 pkg-config 找不到错误 : dav1d >= 0. 2.1

gtk - 如何获取 GtkTextView 的当前字体?

python - Gtk 3,python,appindicator,禁用标签附近的图标

c++ - (GTK :4783): Gtk-CRITICAL **: gtk_image_set_from_pixbuf: assertion 'GTK_IS_IMAGE (image)' failed

gtk - 编译时出错 : gtksourceview/gtksource. h: 没有那个文件或目录

cmake - 如何修复或定位 "ninja: build stopped: subcommand failed."的问题?

linux - 如何使用介子设置 googletest?