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();
}

特性

  1. 跨平台支持:基于Qt后端,支持Windows、Linux和macOS平台
  2. 原生性能:利用Qt的渲染引擎提供高性能UI
  3. 声明式UI:使用Slint的声明式语法构建用户界面
  4. 双向数据绑定:简化UI状态管理

许可证

该crate采用以下许可证:

  • GPL-3.0-only
  • 或 LicenseRef-Slint-Royalty-free-2.0
  • 或 LicenseRef-Slint-Software-3.0

所有者

  • Olivier Goffart
  • Simon Hausmann

1 回复

Rust UI框架Slint的Qt后端插件i-slint-backend-qt:实现跨平台原生GUI应用开发

介绍

Slint是一个轻量级的Rust原生UI框架,专注于提供高效、现代化的用户界面开发体验。i-slint-backend-qt是Slint框架的一个Qt后端插件,它允许开发者使用Slint的声明式UI语言编写界面,同时利用Qt的强大功能实现跨平台原生GUI应用开发。

这个组合提供了以下优势:

  • 使用Rust的安全性和性能
  • Slint简洁的声明式UI语法
  • Qt成熟的跨平台支持
  • 原生外观和性能

使用方法

1. 添加依赖

首先,在Cargo.toml中添加必要的依赖:

[dependencies]
slint = "1.0"
i-slint-backend-qt = "1.0"

2. 基本示例

创建一个简单的窗口应用:

use slint::slint;

slint! {
    import { Button, VerticalBox } from "std-widgets.slint";

    export component MainWindow inherits Window {
        VerticalBox {
            alignment: center;
            Button {
                text: "Click me!";
                clicked => {
                    self.text = "Clicked!";
                }
            }
        }
    }
}

fn main() {
    // 使用Qt后端
    i_slint_backend_qt::init();
    
    let main_window = MainWindow::new().unwrap();
    main_window.run().unwrap();
}

3. 高级功能示例

自定义样式和主题

slint! {
    import { Button, VerticalBox } from "std-widgets.slint";

    export component MainWindow inherits Window {
        in-out property <string> theme: "light";
        
        VerticalBox {
            Button {
                text: "Toggle Theme";
                clicked => {
                    root.theme = root.theme == "light" ? "dark" : "light";
                }
            }
            Button {
                text: "Styled Button";
                style: "custom";
            }
        }
    }
}

fn main() {
    i_slint_backend_qt::init();
    
    let main_window = MainWindow::new().unwrap();
    main_window.set_theme(slint::Theme::Light); // 或 Dark
    main_window.run().unwrap();
}

与Qt功能集成

slint! {
    export component MainWindow inherits Window {
        callback show-message(string);
        
        VerticalBox {
            Button {
                text: "Show Qt Message Box";
                clicked => {
                    root.show-message("Hello from Slint with Qt!");
                }
            }
        }
    }
}

fn main() {
    i_slint_backend_qt::init();
    
    let main_window = MainWindow::new().unwrap();
    
    main_window.on_show_message(|msg| {
        use i_slint_backend_qt::qt;
        
        let message_box = qt::QMessageBox::new();
        message_box.set_text(msg);
        message_box.exec();
    });
    
    main_window.run().unwrap();
}

完整示例demo

以下是一个完整的Slint+Qt应用示例,包含主题切换、Qt对话框集成和自定义样式:

use slint::slint;

// 定义UI组件
slint! {
    import { Button, VerticalBox, LineEdit } from "std-widgets.slint";

    export component MainWindow inherits Window {
        // 主题属性
        in-out property <string> theme: "light";
        
        // 回调定义
        callback show-message(string);
        callback open-file();
        
        // 主布局
        VerticalBox {
            spacing: 5px;
            padding: 10px;
            
            // 主题切换按钮
            Button {
                text: "Toggle Theme";
                clicked => {
                    root.theme = root.theme == "light" ? "dark" : "light";
                }
            }
            
            // 自定义样式按钮
            Button {
                text: "Styled Button";
                style: "custom";
            }
            
            // 文本输入框
            LineEdit {
                placeholder-text: "Enter text here";
            }
            
            // 显示消息按钮
            Button {
                text: "Show Qt Message Box";
                clicked => {
                    root.show-message("Hello from Slint with Qt!");
                }
            }
            
            // 打开文件按钮
            Button {
                text: "Open File Dialog";
                clicked => {
                    root.open-file();
                }
            }
        }
    }
}

fn main() {
    // 初始化Qt后端
    i_slint_backend_qt::init();
    
    // 创建主窗口
    let main_window = MainWindow::new().unwrap();
    
    // 设置初始主题
    main_window.set_theme(slint::Theme::Light);
    
    // 处理消息框回调
    main_window.on_show_message(|msg| {
        use i_slint_backend_qt::qt;
        
        // 创建Qt消息框
        let message_box = qt::QMessageBox::new();
        message_box.set_text(msg);
        message_box.exec();
    });
    
    // 处理文件对话框回调
    main_window.on_open_file(|| {
        use i_slint_backend_qt::qt;
        
        // 创建Qt文件对话框
        let file_dialog = qt::QFileDialog::new();
        if file_dialog.exec() == qt::QDialog::Accepted {
            println!("Selected file: {:?}", file_dialog.selected_files());
        }
    });
    
    // 运行应用
    main_window.run().unwrap();
}

跨平台支持

i-slint-backend-qt支持以下平台:

  • Windows (x86, x86_64)
  • macOS (x86_64, ARM)
  • Linux (x86_64, ARM)

性能优化技巧

  1. 使用Lazy组件延迟加载不立即需要的UI部分
  2. 对于复杂列表,使用for而不是重复组件
  3. 将频繁更新的部分隔离到单独的组件中

注意事项

  • 需要安装Qt开发环境
  • 在Linux上可能需要安装额外的Qt依赖
  • 对于简单的应用,纯Slint后端可能更轻量

这个组合为Rust开发者提供了一个强大的工具,可以创建具有原生外观和性能的跨平台GUI应用程序,同时享受Rust的安全性和Slint的开发效率。

回到顶部