[英]How do I fix my compatability errors when using plotters::CairoBackend with GTK
I am trying to draw an image on a very basic GUI.我正在尝试在非常基本的 GUI 上绘制图像。 I am using GTK for the GUI and want to use the plotters crate for the drawing but am having issues initiating the Backend that plotters uses.我将 GTK 用于 GUI,并希望使用绘图仪板条箱进行绘图,但在启动绘图仪使用的后端时遇到问题。
I have successfully managed to use the BitMapBackend to draw to a file - there is no shortage of examples on the inte.net for this.我已经成功地使用 BitMapBackend 绘制到一个文件——在 inte.net 上不乏这方面的例子。 However, I cannot for the life of me find an example of how to use the CairoBackend to draw onto a gtk DrawingArea.但是,我终生找不到如何使用 CairoBackend 绘制到 gtk DrawingArea 的示例。
My issue arises when I try to connect the draw event to the GTK DrawingArea.当我尝试将绘图事件连接到 GTK DrawingArea 时,我的问题出现了。 I use the connect_draw method which works as expected returning a context to pass to the CairoBackEnd.我使用 connect_draw 方法,该方法按预期返回上下文以传递给 CairoBackEnd。 The problem is that the type of context returned by connect_draw is not compatible with the type of context required by the CairoBackend.问题在于 connect_draw 返回的上下文类型与 CairoBackend 所需的上下文类型不兼容。 I receive context of type 'gtk::cairo::Context' (defined in cairo-rs v0.16.7) but the backend requires a context of 'cairo::context:Context' (defined in cairo-rs v0.15.12).我收到 'gtk::cairo::Context' 类型的上下文(在 cairo-rs v0.16.7 中定义),但后端需要 'cairo::context:Context' 的上下文(在 cairo-rs v0.15.12 中定义)。 Clearly my dependencies are using different versions of the cairo-rs crate.很明显,我的依赖项使用了不同版本的 cairo-rs crate。 Presumably, the versions of my dependencies are not compatible with each other.据推测,我的依赖项的版本彼此不兼容。 How can I fix this?我怎样才能解决这个问题? How do I find out what version of each dependency will work with the others?我如何找出每个依赖项的哪个版本可以与其他依赖项一起使用? My cargo.toml file contains:我的 cargo.toml 文件包含:
[package]
name = "testing3"
version = "0.1.0"
edition = "2021"
[dependencies]
gtk = "0.16.2"
plotters = "0.3.4"
plotters-cairo = "0.3.2"
My rust code is:我的 rust 代码是:
use gtk::prelude::*;
use plotters::prelude::*;
use gtk::{Application, ApplicationWindow};
fn main() {
let app = Application::builder().build();
app.connect_activate(build_gui);
app.run();
}
fn build_gui(app: &Application){
let win = ApplicationWindow::builder().application(app).default_width(320).default_height(200).title("Test").build();
let da: gtk::DrawingArea = gtk::DrawingArea::builder().build();
da.connect_draw(move|_, c|{
let root = plotters_cairo::CairoBackend::new(c, (1024, 768)).unwrap().into_drawing_area();
gtk::Inhibit(false)
});
win.add(&da);
win.show_all();
}
my error message is:我的错误信息是:
--> src/main.rs:21:49
|
21 | let root = plotters_cairo::CairoBackend::new(c, (1024, 768)).unwrap().into_drawing_area();
| --------------------------------- ^ expected struct `cairo::context::Context`, found struct `gtk::cairo::Context`
| |
| arguments to this function are incorrect
|
= note: struct `gtk::cairo::Context` and struct `cairo::context::Context` have similar names, but are actually distinct types
note: struct `gtk::cairo::Context` is defined in crate `cairo`
--> /home/maindesktop/.cargo/registry/src/github.com-1ecc6299db9ec823/cairo-rs-0.16.7/src/context.rs:72:1
|
72 | pub struct Context(ptr::NonNull<cairo_t>);
| ^^^^^^^^^^^^^^^^^^
note: struct `cairo::context::Context` is defined in crate `cairo`
--> /home/maindesktop/.cargo/registry/src/github.com-1ecc6299db9ec823/cairo-rs-0.15.12/src/context.rs:72:1
|
72 | pub struct Context(ptr::NonNull<cairo_t>);
| ^^^^^^^^^^^^^^^^^^
= note: perhaps two different versions of crate `cairo` are being used?
note: associated function defined here
--> /home/maindesktop/.cargo/registry/src/github.com-1ecc6299db9ec823/plotters-cairo-0.3.2/src/backend.rs:70:12
|
70 | pub fn new(context: &'a CairoContext, (w, h): (u32, u32)) -> Result<Self, CairoError> {
| ^^^
For more information about this error, try `rustc --explain E0308`.
error: could not compile `testing3` due to previous error
------------------
(program exited with code: 101)
So with more digging around I have found the solution.因此,通过更多的挖掘,我找到了解决方案。
As I identified from the error message, my code had a compatibility problem.正如我从错误消息中确定的那样,我的代码存在兼容性问题。 The gtk v0.16.2 dependency in my toml file depended itself on cairo-rs v0.16.7 where as the plotters-cairo v0.3.2 depended on cairo-rs v15.12.我的 toml 文件中的 gtk v0.16.2 依赖项本身依赖于 cairo-rs v0.16.7,而 plotters-cairo v0.3.2 依赖于 cairo-rs v15.12。 Clearly, I had to use an earlier version of the gtk dependency - one that depended on cairo-rs v15.12.显然,我不得不使用 gtk 依赖项的早期版本 - 一个依赖于 cairo-rs v15.12 的版本。 But which one.但是哪一个。
On the Doc.rs website I could find the gtk crate documents on https://docs.rs/gtk/0.6.0/gtk/ .在 Doc.rs 网站上,我可以在https://docs.rs/gtk/0.6.0/gtk/找到 gtk crate 文档。 A tab on top left, gtk-0.6.0, on being selected provided both dependency data and versioning options.左上角的选项卡 gtk-0.6.0 在被选中时提供了依赖数据和版本控制选项。 I could see that the latest version of the gtk crate did indeed depend on cairo-rs 0.6.0.我可以看到 gtk crate 的最新版本确实依赖于 cairo-rs 0.6.0。 Selecting the various versions I was able to find the latest version that still depended on cairo-rs v15.xx.选择各种版本后,我找到了仍然依赖于 cairo-rs v15.xx 的最新版本。 In this case it was gtk v0.15.5.在这种情况下,它是 gtk v0.15.5。 Once I replaced my gtk dependency with that version, all was good.一旦我用那个版本替换了我的 gtk 依赖项,一切都很好。 So my new toml was:所以我的新 toml 是:
[package]
name = "testing3"
version = "0.1.0"
edition = "2021"
[dependencies]
gtk = "0.15.5"
plotters = "0.3.4"
plotters-cairo = "0.3.2"
Now by adding the code:现在通过添加代码:
c.rectangle(10.0, 10.0, 70.0, 70.0);
c.fill().expect("Drawing failed");
root.fill(&RED).unwrap();
above my gtk::Inhibit(false) statement, I could indeed create a red rectangle在我的 gtk::Inhibit(false) 语句上方,我确实可以创建一个红色矩形
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.