在Rust中使用GtkSourceView

问题描述 投票:1回答:1

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

我发现 这个 并试图将其包含在我的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>
rust gtk
1个回答
3
投票

我可以通过添加以下一行来运行程序。

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 = "*"
© www.soinside.com 2019 - 2024. All rights reserved.