gpt4 book ai didi

rust - 将GtkSourceView与Rust一起使用

转载 作者:行者123 更新时间:2023-12-03 11:28:59 25 4
gpt4 key购买 nike

我想在我的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>

最佳答案

我可以通过添加以下行来运行该程序:

sourceview::View::static_type();

完整的示例代码:

use gtk::prelude::*;

fn main() {
if gtk::init().is_err() {
println!("Failed to initialize GTK.");
return;
}

sourceview::View::static_type();

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.toml包含以下依赖项:
[dependencies]
gtk = { version = "*", features = ["v3_22"] }
sourceview = "*"

关于rust - 将GtkSourceView与Rust一起使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61493245/

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