表达式与语句
表达式是在用户界面中声明关系和连接的强大方式。 用它们把基本算术与对其他元素属性的访问结合起来。 当这些属性变化时,表达式会自动重新求值, 并把新值赋给与该表达式关联的属性:
export component Example { // declare a property of type int in-out property<int> my-property;
// This accesses the property width: root.my-property * 20px;
}slint
当 my-property 变化时,宽度也会自动变化。
通过元素的名字,后跟 . 和属性名来访问元素的属性:
export component Example { foo := Rectangle { x: 42px; } x: foo.x;}slint
表达式支持你从其他语言中熟知的算术、比较和逻辑运算符,
用 + 进行字符串拼接,以及三元运算符 ... ? ... : ...:
export component Example inherits Window { preferred-width: 100px; preferred-height: 100px;
Rectangle { touch := TouchArea {} background: touch.pressed ? #111 : #eee; border-width: 5px; border-color: !touch.enabled ? #888 : touch.pressed ? #aaa : #555; }}slint
关于全部运算符及其确切语义,参见语言参考中的运算符章节。
在回调处理程序和函数内部,你编写在被调用时运行的语句——
命令式代码就在这里。给属性赋值、用 let 声明局部变量、用 if/else 分支,
以及调用回调或函数:
clicked => { let delta = 42; // local, immutable variable if (condition) { some-property += delta; } else { root.some-callback(); }}slint
关于每种语句形式,参见语言参考中的语句。
© 2026 SixtyFPS GmbH