gpt4 book ai didi

gtk - Vala GtkTemplate : UI Resource not found

转载 作者:行者123 更新时间:2023-12-04 10:18:12 25 4
gpt4 key购买 nike

我正在尝试使用 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");
}
}
}

用户界面/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.xmlui/目录。然后更改prefix/ui .这意味着内存文件系统将使用与 GtkTemplate 匹配的名称。您在 Vala 代码中使用的属性。同时删除 ui/从文件名给出一个平面目录结构:

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

您还需要修改 gresources 的定义在 meson.build文件:
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 只需要在一个共同的祖先目录中。这使得像这样的资源目录结构成为可能:
resources/
├── components/
│   └── zoom-bar.ui
├── icons/
│   └── project.svg
├── layouts/
│   └── main-window.ui
├── themes/
│   ├── dark.css
│   └── light.css
└── filelist.gresource.xml
filelist.gresource.xml文件需要一个 prefix/ ,但子目录从源文件复制到内存文件:

<?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")]
source_dir gnome.compile_resources() 的论据在 meson.build仍然需要设置:
gresources = gnome.compile_resources(
'project-resources',
'resources/filelist.gresource.xml',
source_dir: ['resources']
)

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

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com