当前位置: 首页 > news >正文

QML 状态系统

QML 的状态系统(State System)是一种强大的机制,允许您根据不同的条件或用户交互来改变用户界面的外观和行为。以下是关于 QML 状态系统的详细介绍:

基本概念

  1. 状态(States): 表示组件在不同条件下的不同配置

  2. 转换(Transitions): 定义状态之间变化时的动画效果

  3. 属性更改(Property Changes): 状态切换时发生的属性变化

基本用法 

Item {id: myItemwidth: 100; height: 100// 默认状态state: "normal"// 定义状态states: [State {name: "normal"PropertyChanges { target: myItem; color: "blue" }},State {name: "pressed"PropertyChanges { target: myItem; color: "red" }}]// 定义状态转换动画transitions: [Transition {from: "normal"; to: "pressed"ColorAnimation { duration: 200 }},Transition {from: "pressed"; to: "normal"ColorAnimation { duration: 200 }}]
}

核心元素

元素/属性类型描述
StateQML类型定义一个状态
states属性组件的状态列表
state属性当前状态名称
transitions属性状态转换动画列表
TransitionQML类型定义状态间的过渡动画

State 属性

属性类型描述
namestring状态的唯一标识名称
whenbool条件表达式,为true时自动激活状态
extendstring继承另一个状态的属性更改

State 方法

方法参数描述
PropertyChangestarget: Item, properties...指定状态激活时要改变的属性
StateChangeScriptname: string, script: string状态激活时执行的JS代码
ParentChangetarget: Item, parent: Item改变项目的父项
AnchorChangestarget: Item, anchors...改变项目的锚定设置

Transition 属性

属性类型描述
fromstring起始状态("*"表示任意状态)
tostring目标状态("*"表示任意状态)
reversiblebool是否自动反向播放动画
animationslist包含的动画列表

常用信号

信号描述
onStateChanged当state属性改变时触发

示例代码

qml

Rectangle {id: rectwidth: 100; height: 100color: "blue"states: [State {name: "red_state"PropertyChanges { target: rect; color: "red" }PropertyChanges { target: rect; width: 200 }},State {name: "green_state"when: mouseArea.containsMousePropertyChanges { target: rect; color: "green" }}]transitions: [Transition {from: "*"; to: "red_state"ColorAnimation { duration: 500 }NumberAnimation { properties: "width"; duration: 300 }},Transition {to: "green_state"ColorAnimation { duration: 200 }}]MouseArea {id: mouseAreaanchors.fill: parenthoverEnabled: trueonClicked: rect.state = (rect.state == "red_state" ? "" : "red_state")}onStateChanged: console.log("State changed to:", state)
}

状态定义

状态通过 states 属性定义,每个状态包含:

  • name: 状态的唯一标识符

  • PropertyChanges: 指定在该状态下哪些属性应该改变

  • StateChangeScript: 可以在状态激活时执行 JavaScript 代码

  • ParentChange: 改变项目的父项

  • AnchorChanges: 改变项目的锚定

状态切换

可以通过以下方式切换状态:

  1. 直接设置 state 属性:

    qml

    myItem.state = "pressed"
  2. 使用 MouseArea 交互:

    qml

    MouseArea {anchors.fill: parentonPressed: myItem.state = "pressed"onReleased: myItem.state = "normal"
    }

状态系统工作流程

  1. 定义多个Statestates列表中

  2. 通过设置state属性或when条件切换状态

  3. 状态切换时自动应用PropertyChanges

  4. 通过transitions定义状态切换时的动画效果

  5. 状态改变时会触发onStateChanged信号

高级特性

1. 默认状态

qml

state: "stateName" // 设置初始状态

2. 通配符转换

qml

transitions: Transition {to: "*" // 应用到所有状态变化NumberAnimation { properties: "x,y"; duration: 500 }
}

3. 条件状态

qml

states: State {name: "hidden"when: checkbox.checked // 当条件为 true 时自动激活该状态PropertyChanges { target: rect; opacity: 0 }
}

4. 组合状态

qml

states: [State {name: "left"; when: position.x < 100PropertyChanges { target: rect; color: "green" }},State {name: "right"; when: position.x > 200PropertyChanges { target: rect; color: "red" }}
]

实用技巧

  1. 使用空字符串""表示默认/基础状态

  2. when属性可以实现自动状态切换

  3. 通配符"*"可以匹配任意状态

  4. 多个PropertyChanges可以应用于同一个目标

  5. 使用ScriptAction可以在过渡过程中执行脚本

相关文章:

  • Pycharm 如何删除某个 Python Interpreter
  • Arduino示例代码讲解:Project 07 - Keyboard 键盘
  • FastAPI-MCP
  • C++运算符重载详解
  • Vue3 + TypeScript,使用祖先传后代模式重构父传子模式
  • 【滑动窗口】串联所有单词的⼦串(hard)
  • 多态的主要好处与不足
  • 10.QT-显示类控件|LCD Number|ProgressBar|Calendar Widget(C++)
  • [论文阅读]Making Retrieval-Augmented Language Models Robust to Irrelevant Context
  • 论文阅读:2024 arxiv DeepInception: Hypnotize Large Language Model to Be Jailbreaker
  • Pandas高级功能
  • C++入门篇(下)
  • 【支付】支付宝支付
  • go+mysql+cocos实现游戏搭建
  • centos停服 迁移centos7.3系统到新搭建的openEuler
  • HTMLCSS实现网页轮播图
  • max31865典型电路
  • 经典算法 表达式求值
  • DEA-Net:基于细节增强卷积和内容引导注意力的单图像去雾
  • 基础理论学习参考
  • 女子“伸腿阻拦高铁关门”被行拘,事件追踪:当时发生了什么?
  • 南部战区回应菲护卫艇非法侵入中国黄岩岛领海:依法警告驱离
  • 数智时代出版专业技能人才培养研讨会在沪举行
  • 白宫慌了!将设工作组紧急处理对中国加征关税危机
  • 商务部新闻发言人就美对我海事、物流和造船等领域宣布最终措施答记者问
  • 秦洪看盘|A股缩量窄幅震荡,短线或延续有韧性、无弹性走势