new_from_file() 在 GTK 中——我总是需要那个 "glade" 文件吗?我可以嵌入它吗?
new_from_file() in GTK -- do I always need that "glade" file? Can I embed it or something?
在我的 Rust GTK 应用程序中有这个,但是,我认为它也适用于 C++:
let builder = Builder::new_from_file("main_window.glade");
当我在发布模式甚至在调试模式下编译我的应用程序时,我想摆脱使用文件 "main_window.glade" 分发可执行文件的必要性。
如何link或将 glade 文件静态嵌入到可执行文件中?
我很确定你要找的是 GResource which is part of GIO. You have to construct an XML file as explained in the description of the object, and then compile it using glib-compile-resources. Here is another page which calls out something like a typical workflow for GResource use. A similar stack overflow question was also asked and answered here。
Rust 标准库提供了 include_str!
宏,它允许您将文件内容作为静态字符串 (&'static str
) 包含到您的程序中,然后您可以将其分配给全局变量常数。
const MAIN_WINDOW: &'static str = include_str!("main_window.glade");
然后,您可以使用 Builder::new_from_string
使用该字符串构建 UI:
let builder = Builder::new_from_string(MAIN_WINDOW);
如果你只想使用常量一次,那么你可以直接使用include_str!
:
let builder = Builder::new_from_string(include_str!("main_window.glade"));
在我的 Rust GTK 应用程序中有这个,但是,我认为它也适用于 C++:
let builder = Builder::new_from_file("main_window.glade");
当我在发布模式甚至在调试模式下编译我的应用程序时,我想摆脱使用文件 "main_window.glade" 分发可执行文件的必要性。
如何link或将 glade 文件静态嵌入到可执行文件中?
我很确定你要找的是 GResource which is part of GIO. You have to construct an XML file as explained in the description of the object, and then compile it using glib-compile-resources. Here is another page which calls out something like a typical workflow for GResource use. A similar stack overflow question was also asked and answered here。
Rust 标准库提供了 include_str!
宏,它允许您将文件内容作为静态字符串 (&'static str
) 包含到您的程序中,然后您可以将其分配给全局变量常数。
const MAIN_WINDOW: &'static str = include_str!("main_window.glade");
然后,您可以使用 Builder::new_from_string
使用该字符串构建 UI:
let builder = Builder::new_from_string(MAIN_WINDOW);
如果你只想使用常量一次,那么你可以直接使用include_str!
:
let builder = Builder::new_from_string(include_str!("main_window.glade"));