【发布时间】:2020-04-30 04:42:59
【问题描述】:
我想在我的 Rust 应用程序中使用 GtkSourceView。
我使用 Glade 创建了 UI 文件。我必须安装组件并链接它。
这是我的 Rust 代码:
use gtk::prelude::*;
fn main() {
if gtk::init().is_err() {
println!("Failed to initialize GTK.");
return;
}
let glade_src = include_str!("user_interface.glade");
let builder = gtk::Builder::new_from_string(glade_src);
let window: gtk::Window = builder.get_object("main_window").unwrap();
window.show_all();
gtk::main();
}
...以及我在尝试cargo run 时遇到的错误:
alex@smartalex-bed:~/.repos/codelib/rust/venom$ cargo run
Finished dev [unoptimized + debuginfo] target(s) in 0.05s
Running `target/debug/venom`
(venom:10028): Gtk-ERROR **: 04:25:30.689: failed to add UI: .:17:1 Invalid object type 'GtkSourceView'
Trace/breakpoint trap (core dumped)
我找到了this 并尝试将其包含在我的 Cargo.toml 中,但错误仍然存在。
如何在我的 Rust 应用程序中使用 GtkSourceView?
这是user_interface.glade:
<?xml version="1.0" encoding="UTF-8"?>
<!-- Generated with glade 3.22.2 -->
<interface>
<requires lib="gtk+" version="3.20"/>
<requires lib="gtksourceview" version="4.0"/>
<object class="GtkWindow" id="main_window">
<property name="can_focus">False</property>
<child type="titlebar">
<placeholder/>
</child>
<child>
<object class="GtkBox">
<property name="visible">True</property>
<property name="can_focus">False</property>
<child>
<object class="GtkSourceView">
<property name="visible">True</property>
<property name="can_focus">True</property>
<property name="left_margin">2</property>
<property name="right_margin">2</property>
<property name="monospace">True</property>
<property name="show_line_numbers">True</property>
<property name="show_line_marks">True</property>
<property name="tab_width">4</property>
<property name="auto_indent">True</property>
<property name="insert_spaces_instead_of_tabs">True</property>
<property name="show_right_margin">True</property>
<property name="highlight_current_line">True</property>
<property name="smart_backspace">True</property>
</object>
<packing>
<property name="expand">True</property>
<property name="fill">True</property>
<property name="position">0</property>
</packing>
</child>
<child>
<placeholder/>
</child>
</object>
</child>
</object>
</interface>
【问题讨论】:
-
AFAIK,UI 构建器使用动态链接魔法从库中访问小部件。但是 rust 不会链接库,除非它在您的代码中看到引用的符号。您可以尝试在调用
gtk_init():unsafe { gtk_source_sys::gtk_source_view_get_type() };后添加此行以强制引用吗?不要忘记将箱子gtk-source-sys添加到您的Cargo.toml。 -
上述的安全包装只是
sourceview::View::static_type();