Rust UI框架Slint的Qt后端插件i-slint-backend-qt:实现跨平台原生GUI应用开发
Rust UI框架Slint的Qt后端插件i-slint-backend-qt:实现跨平台原生GUI应用开发
注意:这个库是Slint项目的一个内部crate。使用Slint的应用程序不应直接使用这个crate,而应该使用slint
crate。
警告:这个crate不遵循语义化版本规范,在Cargo.toml中只能使用version = "=x.y.z"
的形式。
安装
在项目目录中运行以下Cargo命令:
cargo add i-slint-backend-qt
或者在Cargo.toml中添加以下行:
i-slint-backend-qt = "1.12.1"
示例代码
以下是一个使用Slint和Qt后端的完整示例:
use slint::SharedString;
fn main() {
// 定义Slint UI
let ui = slint::slint! {
import { Button, VerticalLayout } from "std-widgets.slint";
export component MainWindow inherits Window {
callback button-clicked <=> button.clicked;
in property <string> button_text;
VerticalLayout {
button := Button {
text: button_text;
}
}
}
};
// 创建UI实例
let main_window = ui::MainWindow::new().unwrap();
// 设置按钮文本
main_window.set_button_text(SharedString::from("Click me!"));
// 连接按钮点击信号
main_window.on_button_clicked(|| {
println!("Button was clicked!");
});
// 运行UI
main_window.run().unwrap();
}
完整示例demo
以下是一个更完整的示例,展示了如何使用Slint和Qt后端创建一个简单的计数器应用:
use slint::{SharedString, Model};
slint::slint! {
import { Button, HorizontalLayout, VerticalLayout, Text } from "std-widgets.slint";
export component CounterWindow inherits Window {
property <int> counter: 0;
VerticalLayout {
Text {
text: "当前计数: " + counter;
font-size: 24px;
}
HorizontalLayout {
Button {
text: "+";
clicked => { root.counter += 1; }
}
Button {
text: "-";
clicked => { root.counter -= 1; }
}
Button {
text: "重置";
clicked => { root.counter = 0; }
}
}
}
}
}
fn main() {
let window = CounterWindow::new().unwrap();
// 监听计数器变化
window.on_counter_changed(|value| {
println!("计数器值已更新: {}", value);
});
window.run().unwrap();
}
特性
- 跨平台支持:基于Qt后端,支持Windows、Linux和macOS平台
- 原生性能:利用Qt的渲染引擎提供高性能UI
- 声明式UI:使用Slint的声明式语法构建用户界面
- 双向数据绑定:简化UI状态管理
许可证
该crate采用以下许可证:
- GPL-3.0-only
- 或 LicenseRef-Slint-Royalty-free-2.0
- 或 LicenseRef-Slint-Software-3.0
所有者
- Olivier Goffart
- Simon Hausmann
1 回复