全局对象
用 global Name { /* .. properties or callbacks .. */ } 声明一个全局单例,
即可让属性和回调在整个项目中可用。
用 Name.property 访问它们。
例如,这对通用的调色板很有用:
global Palette { in-out property<color> primary: blue; in-out property<color> secondary: green;}
export component Example inherits Rectangle { background: Palette.primary; border-color: Palette.secondary; border-width: 2px;}slint
导出全局对象,使其可从其他文件访问。 要从 Rust、C++、JavaScript 或 Python 的业务逻辑中访问某个全局对象—— 设置其属性并实现其回调——请从同时导出主应用程序组件的文件中重新导出它:
export global Logic { in-out property <int> the-value; pure callback magic-operation(int) -> int;}// ...slint
slint::slint!{export global Logic { in-out property <int> the-value; pure callback magic-operation(int) -> int;}
export component App inherits Window { // ...}}
fn main() { let app = App::new(); app.global::<Logic>().on_magic_operation(|value| { eprintln!("magic operation input: {}", value); value * 2 }); app.global::<Logic>().set_the_value(42); // ...}rust
#include "app.h"
int main() { auto app = App::create(); app->global<Logic>().on_magic_operation([](int value) -> int { return value * 2; }); app->global<Logic>().set_the_value(42); // ...}C++
import * as slint from 'slint-ui';let file = slint.loadFile(new URL('app.slint', import.meta.url));let app = new file.App();app.Logic.magic_operation = (value) => { return value * 2;};app.Logic.the_value = 42;// ...js
import slint
class App(slint.loader.app.App): @slint.callback(global_name="Logic") def magic_operation(self, value: int) -> int: return value * 2
app = App()app.Logic.the_value = 42
# ...python
要用双向绑定在组件上重新暴露全局属性,参见语言参考中的 全局对象章节。
© 2026 SixtyFPS GmbH