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

qml之动态元素类型

文章目录

  • 动画
    • 例子
  • 应用动画
    • 例子
  • 缓动曲线
    • 例子
  • 动画分组
    • 例子
  • 嵌套动画
    • 代码
  • 状态和转换
    • 代码

动画

  • QMlL使用插值的方式控制属性的更改。
  • 动画是在指定的时间内一些列属性的持续变化。
    常用的动画类型元素动画:
  • PropertyAnimation:属性值改变播放动画
  • NumberAnimation:qreal_type值改变播放动画
  • ColorAnimation:颜色值改变播放动画
  • RotationAnimation:旋转值改变播放的动画
    Qt Quick还提供了一些特殊场景下需要使用的动画类型:
    PauseAnimation,SequentialAnimation,ParallelAnimation,AnchorAnimation,ParentAnimation,SmoothedAnimation ,SpringAnimation,PathAnimation,Vector3dAnimation
    对于更加复杂的动画,可能需要在播放动画时改变属性或者运行脚本。为此,QtQuick提供了action元素:
  • PropertyAction:在播放动画时改变属性
  • ScripAction:在播放动画时运行脚本

例子

import QtQuick
import QtQuick.WindowImage {id: rootsource: "../../images/background.png"property int padding: 40property bool running: falseImage {id: qqsource: "../../images/qq.png"x:root.padding;y:(root.height-height)/2NumberAnimation on x{to:root.width-qq.width-root.paddingduration: 3000running: root.running}RotationAnimator on rotation{to:360duration: 3000running: root.running}OpacityAnimator on opacity{to:0duration: 3000running: root.running}}MouseArea{anchors.fill: rootonClicked: root.running = true}
}

在这里插入图片描述

  • 应用于x和rotation、透明度属性的简单动画。
  • 每个动画的持续时间为3000毫秒。
  • x:将对象逐渐移动到右边的位置。
  • rotation:从当前角度运行到360度。
  • 透明度:从1到0
  • 三个动画并行运行,并在单击鼠标区域时启动。

应用动画

可以通过多种方式执行动画:

  • 属性上的动画:在元素完全加载后自动运行
  • 属性上的行为:属性值更改时自动运行
  • 独立动画:使用start()显式启动动画或将running设置为true时运行

例子

import QtQuick
import QtQuick.WindowWindow {id:rootwidth: 640height: 480visible: truetitle: qsTr("Hello World")color:"gray"ClickableImageV2{id:qq1x:40;y:root.height-heightsource:"../../images/qq.png"text:"animation on property"NumberAnimation on y{to:40;duration:3000}}ClickableImageV2{id:qq2x:40+qq1.width+20;y:root.height-heightsource:"../../images/qq.png"text:"animation on property"Behavior on y{NumberAnimation{duration:3000}}onClicked: y=40}ClickableImageV2{id:qq3x:40+qq1.width+qq2.x;y:root.height-heightsource:"../../images/qq.png"text:"animation on property"NumberAnimation{id:animtarget:qq3from:root.height-qq3.heightto:40;duration:3000property:"y"running:area.pressed}MouseArea{id:areaanchors.fill: parent}}
}

 第一个对象使用on<property>策略进行移动。动画立即开始。
 第二个对象使用Behavior on动画。此行为告诉属性它应该为值的每个更改设置动画。可以通过在行为元素上设置enabled:false来禁用该行为。
 第三个对象使用standalone动画。动画被定义为其自己的元素,几乎可以位于文档中的任何位置。

缓动曲线

在这里插入图片描述
属性的值更改可以由动画控制。缓动属性允许影响属性更改的插值曲线。
y轴:property
x轴:duration

例子

import QtQuick
import QtQuick.Window
import QtQuick.LayoutsRectangle{id:rootwidth: childrenRect.widthheight: childrenRect.heightcolor:'gray'gradient: Gradient{GradientStop{position:0.0;color:root.color}GradientStop{position:1.0;color:Qt.lighter(root.color,1.5)}}ColumnLayout{spacing: 20Grid{spacing: 10columns:5EasingType{title:'Linear'easingType: Easing.LinearonClicked: {animation.easing.type=easingTypebox.toggle=!box.toggle}}EasingType{title:'InExpo'easingType: Easing.InExpoonClicked: {animation.easing.type=easingTypebox.toggle=!box.toggle}}EasingType{title:'OutExpo'easingType: Easing.OutExpoonClicked: {animation.easing.type=easingTypebox.toggle=!box.toggle}}EasingType{title:'InOutExpo'easingType: Easing.InOutExpoonClicked: {animation.easing.type=easingTypebox.toggle=!box.toggle}}EasingType{title:'InOutCubic'easingType: Easing.InOutCubiconClicked: {animation.easing.type=easingTypebox.toggle=!box.toggle}}EasingType{title:'SineCurve'easingType: Easing.SineCurveonClicked: {animation.easing.type=easingTypebox.toggle=!box.toggle}}EasingType{title:'InOutCirc'easingType: Easing.InOutCirconClicked: {animation.easing.type=easingTypebox.toggle=!box.toggle}}EasingType{title:'InOutElastic'easingType: Easing.InOutElasticonClicked: {animation.easing.type=easingTypebox.toggle=!box.toggle}}EasingType{title:'InOutBack'easingType: Easing.InOutBackonClicked: {animation.easing.type=easingTypebox.toggle=!box.toggle}}EasingType{title:'InOutBounce'easingType: Easing.InOutBounceonClicked: {animation.easing.type=easingTypebox.toggle=!box.toggle}}}Rectangle{height:100Layout.fillWidth: truegradient: Gradient{GradientStop{position:0.0;color:'gray'}GradientStop{position:1.0;color:'green'}}Rectangle{id:boxproperty  bool toggleanchors.verticalCenter: parent.verticalCenterwidth: 80;height:80gradient: Gradient{GradientStop{position:0.0;color:'red'}GradientStop{position:1.0;color:'yellow'}}x:toggle?20:root.width-width-20Behavior on x{NumberAnimation{id:animationduration:1000}}}}}}

在这里插入图片描述
点击不同的曲线会有不同的动画效果。

动画分组

 分组有两种方式:并行或顺序。
可以使用SequentialAnimation或ParallelAnimation元素,它们充当其他动画元素的动画容器。这些分组动画本身就是动画。
在这里插入图片描述

例子

import QtQuick
import QtQuick.WindowWindow {id:rootwidth: 640height: 480visible: truetitle: qsTr("UFO")property int duration: 3000Image {source: "../../images/background.png"anchors.fill: parent}ClickableImageV3{id:ufox:20;y:root.height-heightsource: "../../images/ufo.png"text:'UFO'onClicked: anim.restart()}ParallelAnimation/*SequentialAnimation*/{id:animNumberAnimation{target: ufoproperty: 'y'from:root.height-ufo.heightto:20duration: root.duration}NumberAnimation{target: ufoproperty: 'x'from:20to:500duration: root.duration}}
}

在这里插入图片描述

嵌套动画

 分组动画也可以嵌套。例如,一个连续动画可以有两个并行动画作为子动画。我们可以通过一个足球示例:

  • 从左到右的x平移(X1)
  • 从下到上的y平移(Y1),然后是从上到下的平移(Y2),带有一些弹跳
  • 在动画的整个持续时间内旋转360度(ROT1)
    在这里插入图片描述
     即我们可以将y的改变分成一次顺序动画,角度和x的变化与这次顺序动画为一个并行动画即可实现效果。

代码

import QtQuick
import QtQuick.WindowItem {id:rootwidth: 480height: 300property int duration: 3000Rectangle{id:skywidth: root.widthheight: 200gradient: Gradient{GradientStop{position:0.0;color:"#0080FF"}GradientStop{position:1.0;color:"#66CCFF"}}}Rectangle{id:groundanchors.top: sky.bottomanchors.bottom:root.bottomwidth: root.widthgradient: Gradient{GradientStop{position:0.0;color:"#00FF00"}GradientStop{position:1.0;color:"#00803F"}}}Image {id: ballsource: "../../images/soccer_ball.png"scale:0.5x:0;y:root.height-heightMouseArea{anchors.fill: parentonClicked: {ball.x=0ball.y=root.height-ball.heightball.rotation=0anim.restart()}}}ParallelAnimation{id:animSequentialAnimation{NumberAnimation{properties: "y"target: ballto:20duration: root.duration*0.4easing.type:Easing.OutCirc}NumberAnimation{properties: "y"target: ballto:root.height-ball.heightduration: root.duration*0.6easing.type:Easing.OutBounce}}NumberAnimation{properties: "x"target: ballto:380duration: root.duration}RotationAnimation{properties: "rotation"target: ballto:720duration: root.duration}}
}

状态和转换

  • 状态定义了一组属性的更改,可以由特定条件触发。
  • 状态开关可以附加一个转换,该转换定义了这些更改对应的动画,或执行附加的行为。
  • 进入状态时也可以执行行为。
    例如,两个信号灯。stop用红色,go用绿色。两个灯光不应同时发光。
state: "stop" states: [ State { name: "stop" PropertyChanges { target: light1; color: root.red }PropertyChanges { target: light2; color: root.black } }, State { name: "go" PropertyChanges { target: light1; color: root.black } PropertyChanges { target: light2; color: root.green } } 
] MouseArea { anchors.fill: parent onClicked: parent.state = (parent.state == "stop" ? "go" : "stop") 
} 

在这里插入图片描述
 现在能够成功地改变信号灯的状态。为了使UI更具吸引力,应该添加一些带有动画效果的过渡。状态改变可以触发转换。

transitions: [ Transition { from: "stop"; to: "go" // from: "*"; to: "*" ColorAnimation { target: light1; properties: "color"; duration: 2000 } ColorAnimation { target: light2; properties: "color"; duration: 2000 } } 
] 

rom: “"; to: "” 表示“从任何状态到任何其他状态”,是默认值。

代码

import QtQuick
import QtQuick.WindowItem {id: rootwidth: 150;height:260property color black: 'black'property color red: 'red'property color green: 'green'Rectangle{anchors.fill: parentcolor:"#333333"}state: "stop"states: [State {name: "stop"PropertyChanges {target: light1;color:root.red;}PropertyChanges {target: light2;color:root.black;}},State {name: "go"PropertyChanges {target: light1;color:root.black;}PropertyChanges {target: light2;color:root.green;}}]transitions: [Transition {from: "*";to: "*"ColorAnimation {target:light1;duration: 1000;properties: 'color'}ColorAnimation {target:light2;duration: 1000;properties: 'color'}}]Rectangle{id:light1x:25;y:15width:100;height: widthradius: width/2color:root.blackborder.color: Qt.lighter(color,1.1)}Rectangle{id:light2x:25;y:135width:100;height: widthradius: width/2color:root.blackborder.color: Qt.lighter(color,1.1)}MouseArea{anchors.fill: rootonClicked: {parent.state = ((parent.state == "stop")?"go":"stop")}}
}

完整代码链接

相关文章:

qml之动态元素类型

文章目录 动画例子 应用动画例子 缓动曲线例子 动画分组例子 嵌套动画代码 状态和转换代码 动画 QMlL使用插值的方式控制属性的更改。动画是在指定的时间内一些列属性的持续变化。 常用的动画类型元素动画:PropertyAnimation:属性值改变播放动画NumberAnimation:qr…...

超详细 | 差分进化算法原理及其实现(Matlab/Python)

差分进化(Differential Evolution,DE)算法是由美国学者Storn和 Price在1995年为求解Chebyshev多项式拟合问题而提出的。算法主要通过基于差分形式的变异操作和基于概率选择的交叉操作进行优化搜索,虽然其操作名称和遗传算法相同,但实现方法有…...

大二第三周总结(算法+生活)

算法: 题目:有效的括号 这个题目也是做过很多回了。主要就是数据结构中”栈“的应用,先进后出。 解题思路: 1.创建 Map 哈希表形成键值对映射 2.进行遍历字符串 在遍历过程中 如果 遍历到的字符c 是左括号,则入栈 pu…...

Lake Formation 和 IAM 之间的区别与联系

IAM 和 Lake Formation 都是 AWS 上的权限管理服务,且默认都是自动开启并生效的,只是如果你没有特别配置过它们,可能感觉不到它们的存在,特别是Lake Formation(后文简写为 LF),通常情况下都是“透明”的,但它确实在每次请求时进行了权限检查。本文会详细介绍一下两者之…...

音频抓取代码示例

以下是一个使用DefaultsKit库的简单爬虫程序,用于爬取音频。代码中使用了https://www.duoip.cn/get_proxy的API获取代理服务器。 import Foundation import DefaultsKit ​ let url "https://www.douban.com/music" // 目标网站URL let proxyUrl "…...

Hexo搭建个人博客系列之环境准备

环境准备 Git Git官网,安装过程,就是一直下一步,详细的看这篇文章 Git的安装 Node.js Node.js官网 Node.js的安装 注册一个GitHub账号 安装hexo 新建一个文件夹(位置任意),运行cmd(若出现了operation not permitted,就以管理员的权限来运行cmd),运行…...

小程序技术在信创操作系统中的应用趋势:适配能力有哪些?

小程序技术在信创操作系统中的应用前景非常广阔,但也面临着一些挑战和问题。开发者需要积极应对这些挑战和问题,为信创操作系统的发展和推广做出贡献。同时,开发者也需要关注小程序技术在信创操作系统中的应用趋势,积极探索新的应…...

word修改公式默认字体并打出漂亮公式

文章目录 word公式简介传统方法1——mathtype传统方法2——word自带公式编辑器最简洁方法——更改word自带公式字体快捷方式效果展示 word公式简介 word自带的公式字体Cambria Math不可否认很丑,要打出latex格式的漂亮字体很困难。使用Markdown工具很多只能导出为不…...

Day 08 python学习笔记

函数 作用域 作用域:变量的访问权限 全局变量与局部变量 声明在函数外边的变量----全局变量 ----》全局作用域 函数内部的变量------局部变量 ----》局部作用域顶格创建的函数也是全局的 例: a 100def test_01():a 0b 110print…...

Qt Designer如何安装,打开方式

Qt Designer分为PyQt5 Qt Designer、PySide6 Qt Designer,下面分别介绍各自的安装方式和打开方式 首先,检查是否安装了python,使用cmd打开命令行窗口,输入: python --version若出现python的版本号,则已安…...

《Effective C++》知识点(1)--让自己习惯C++

多年前看过的这本书(侯捷翻译的),忘得差不多了,重温复习一下。 1. 视C为一个语言联邦 C并不只是一个带有一组守则的一体语言;它是从四个次语言组成的联邦(federation)政府,每个次语言都有自己的规约。 次语言说明CC是C的基础&am…...

UVM 验证方法学之interface学习系列文章(八)《interface不小心引入X态问题》

前面的文章学习,想必大家都对interface 有了深入了解。大家可不要骄傲哦,俗话说:小心驶得万年船。今天,再给大家介绍一个工作中,不是经常遇到,但是一旦遇到,会让你纠结很久的事情。 前面文章提到,随着验证复杂度的不断增加,interface 的bind 的操作,是必不可少的用法…...

BBR算法的几种状态

BBR(Bottleneck Bandwidth and Round-trip propagation time)算法根据互联网的拥塞行为定义了四种状态:STARTUP、DRAIN、PROBE_BW和PROBE_RTT。下面对每种状态进行详细解释,并说明它们之间的区别: STARTUP(…...

利用jupyter进行分类

Jupyter Notebook是一个非常强大的工具,可以用于各种数据分析和机器学习任务,包括分类问题。在Jupyter Notebook中进行分类通常需要以下步骤: 导入所需的库:首先,你需要导入必要的Python库,例如NumPy、Pand…...

【LeetCode 算法专题突破】滑动窗口(⭐)

文章目录 前言1. 长度最小的子数组题目描述代码 2. 无重复字符的最长子串题目描述代码 3. 最大连续1的个数 III题目描述代码 4. 将 x 减到 0 的最小操作数题目描述代码 5. 水果成篮题目描述代码 6. 找到字符串中所有字母异位词题目描述代码 7. 串联所有单词的子串题目描述代码 …...

按键中断控制LED灯亮灭

EXTI—外部中断/事件控制器 EXTI(External interrupt/event controller)—外部中断/事件控制器,管理了控制器的 20 个中断/事 件线。每个中断/事件线都对应有一个边沿检测器,可以实现输入信号的上升沿检测和下降沿的 检测。EXTI可…...

YOLOV8目标检测——模型训练

文章目录 1下载yolov8([网址](https://github.com/ultralytics/ultralytics))2用pycharm打开文件3训练自己的YOLOV8数据集4run下运行完了之后没有best.pt文件5导出为onnx文件 本章内容主要解决如何训练自己的YOLOV8模型。 1下载yolov8(网址&a…...

利用dockerfile升级flink的curl

最近Nusses扫出flink镜像有CURL漏洞,才发现要更新到最新版本 8.4.0,笔者当时flink版本为: flink:1.17.1-scala_2.12-java8 官方镜像仓库:https://hub.docker.com/_/flinkapt源 我试了如上2种方法,都不能更新curl到8…...

element 日期选择器禁止选择指定日期前后时间

画圈重点&#xff1a;disabledDate的写法要用箭头函数&#xff0c;不能用普通函数写法&#xff0c;否则this指向就错了&#xff0c;会报 undefined <el-date-picker v-model"time" type"date" value-format"yyyy-MM-dd" :…...

00TD时尚女童睡衣,蕾丝边+蝴蝶结太好看了

甜美又可爱的蕾丝花边加蝴蝶结 真的一下子戳中了我的心巴&#xff0c; 满满的少女风真的很好看&#xff0c; 妥妥的可爱小公主一枚 柔软又亲肤&#xff0c;厚厚的很保暖 睡觉真的很舒服 还有袖口和裤脚都做了松紧设计哟&#xff01;...

PHP:Web 开发的强大基石与未来展望

在当今数字化时代&#xff0c;Web 开发技术日新月异&#xff0c;各种编程语言和框架层出不穷。然而&#xff0c;PHP 作为一种历史悠久且广泛应用的服务器端脚本语言&#xff0c;依然在 Web 开发领域占据着重要地位。 PHP 的历史与现状 PHP&#xff08;Hypertext Preprocessor…...

【Linux】为 Git 设置 Commit 提交模板方法,可统一个人或者项目的提交风格

为 Git 设置 Commit 提交模板 新建模板文件。注意之后不能删除该文件。 gedit ~/.gitmessage.txt粘贴自己的模板。可以给 AI 提自己的需求&#xff0c;定制一个模板&#xff0c;例如 # <type>(<scope>): <description> # # [optional body] # # [optional…...

FPGA 动态重构配置流程

触发FPGA 进行配置的方式有两种&#xff0c;一种是断电后上电&#xff0c;另一种是在FPGA运行过程中&#xff0c;将PROGRAM 管脚拉低。将PROGRAM 管脚拉低500ns 以上就可以触发FPGA 进行重构。 FPGA 的配置过程大致可以分为&#xff1a;配置的触发和建立阶段、加载配置文件和建…...

当主观认知遇上机器逻辑:减少大模型工程化中的“主观性”模糊

一、人类与机器的认知差异 当自动驾驶汽车遇到紧急情况需要做出选择时&#xff0c;人类的决策往往充满矛盾&#xff1a;有人会优先保护儿童和老人&#xff0c;有人坚持"不主动变道"的操作原则。这种差异背后&#xff0c;体现着人类特有的情感判断与价值选择。而机器的…...

第3章——SSM整合

一、整合持久层框架MyBatis 1.准备数据库表及数据 创建数据库&#xff1a;springboot 使用IDEA工具自带的mysql插件来完成表的创建和数据的准备&#xff1a; 创建表 表创建成功后&#xff0c;为表准备数据&#xff0c;如下&#xff1a; 2.创建SpringBoot项目 使用脚手架创建…...

git提交代码和解决冲突修复bug

提交到分支的步骤如下&#xff1a; 确保你当前在开发分支上&#xff0c;可以使用命令 git branch 来查看当前所在分支&#xff0c;并使用 git checkout 命令切换到开发分支。使用 git add 命令将修改的文件添加到暂存区。使用 git commit 命令提交代码到本地仓库。 解决合并冲…...

自动化提示生成框架(AutoPrompt)

自动化提示生成框架(AutoPrompt) 一、核心创新点 自动化提示生成框架(AutoPrompt) 创新本质:提出基于梯度引导搜索的自动化提示生成方法,替代人工设计模板的传统模式。技术路径: 将提示视为可训练的离散token序列,通过优化提示向量(prompt embedding)搜索语义空间。利…...

android 之 Tombstone

Android 系统中的 Tombstone 是记录 Native 层崩溃信息的关键日志文件&#xff0c;当应用或系统服务因严重错误&#xff08;如内存访问异常、空指针解引用等&#xff09;崩溃时自动生成。以下是其核心机制与分析方法详解&#xff1a; 一、Tombstone 的生成机制 触发条件 当 Na…...

Delphi 实现远程连接 Access 数据库的指南

方法一&#xff1a;通过局域网共享 Access 文件&#xff08;简单但有限&#xff09; 步骤 1&#xff1a;共享 Access 数据库 将 .mdb 或 .accdb 文件放在局域网内某台电脑的共享文件夹中。 右键文件夹 → 属性 → 共享 → 启用共享并设置权限&#xff08;需允许网络用户读写&a…...

ServBay 1.13.0 更新,新增第三方反向代理/内网穿透

ServBay 作为一款简化本地开发环境搭建与管理的强大工具&#xff0c;致力于打造一个开箱即用、稳定可靠的本地开发平台&#xff0c;让用户专注于代码编写&#xff0c;提升开发效率。 ServBay 1.13.0 正式发布&#xff01;本次更新聚焦于提升本地开发项目的外部可访问性、增强国…...