当前位置: 首页 > 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;...

云原生核心技术 (7/12): K8s 核心概念白话解读(上):Pod 和 Deployment 究竟是什么?

大家好&#xff0c;欢迎来到《云原生核心技术》系列的第七篇&#xff01; 在上一篇&#xff0c;我们成功地使用 Minikube 或 kind 在自己的电脑上搭建起了一个迷你但功能完备的 Kubernetes 集群。现在&#xff0c;我们就像一个拥有了一块崭新数字土地的农场主&#xff0c;是时…...

基于FPGA的PID算法学习———实现PID比例控制算法

基于FPGA的PID算法学习 前言一、PID算法分析二、PID仿真分析1. PID代码2.PI代码3.P代码4.顶层5.测试文件6.仿真波形 总结 前言 学习内容&#xff1a;参考网站&#xff1a; PID算法控制 PID即&#xff1a;Proportional&#xff08;比例&#xff09;、Integral&#xff08;积分&…...

.Net框架,除了EF还有很多很多......

文章目录 1. 引言2. Dapper2.1 概述与设计原理2.2 核心功能与代码示例基本查询多映射查询存储过程调用 2.3 性能优化原理2.4 适用场景 3. NHibernate3.1 概述与架构设计3.2 映射配置示例Fluent映射XML映射 3.3 查询示例HQL查询Criteria APILINQ提供程序 3.4 高级特性3.5 适用场…...

IGP(Interior Gateway Protocol,内部网关协议)

IGP&#xff08;Interior Gateway Protocol&#xff0c;内部网关协议&#xff09; 是一种用于在一个自治系统&#xff08;AS&#xff09;内部传递路由信息的路由协议&#xff0c;主要用于在一个组织或机构的内部网络中决定数据包的最佳路径。与用于自治系统之间通信的 EGP&…...

Docker 运行 Kafka 带 SASL 认证教程

Docker 运行 Kafka 带 SASL 认证教程 Docker 运行 Kafka 带 SASL 认证教程一、说明二、环境准备三、编写 Docker Compose 和 jaas文件docker-compose.yml代码说明&#xff1a;server_jaas.conf 四、启动服务五、验证服务六、连接kafka服务七、总结 Docker 运行 Kafka 带 SASL 认…...

关于nvm与node.js

1 安装nvm 安装过程中手动修改 nvm的安装路径&#xff0c; 以及修改 通过nvm安装node后正在使用的node的存放目录【这句话可能难以理解&#xff0c;但接着往下看你就了然了】 2 修改nvm中settings.txt文件配置 nvm安装成功后&#xff0c;通常在该文件中会出现以下配置&…...

【OSG学习笔记】Day 16: 骨骼动画与蒙皮(osgAnimation)

骨骼动画基础 骨骼动画是 3D 计算机图形中常用的技术&#xff0c;它通过以下两个主要组件实现角色动画。 骨骼系统 (Skeleton)&#xff1a;由层级结构的骨头组成&#xff0c;类似于人体骨骼蒙皮 (Mesh Skinning)&#xff1a;将模型网格顶点绑定到骨骼上&#xff0c;使骨骼移动…...

OpenPrompt 和直接对提示词的嵌入向量进行训练有什么区别

OpenPrompt 和直接对提示词的嵌入向量进行训练有什么区别 直接训练提示词嵌入向量的核心区别 您提到的代码: prompt_embedding = initial_embedding.clone().requires_grad_(True) optimizer = torch.optim.Adam([prompt_embedding...

使用 Streamlit 构建支持主流大模型与 Ollama 的轻量级统一平台

🎯 使用 Streamlit 构建支持主流大模型与 Ollama 的轻量级统一平台 📌 项目背景 随着大语言模型(LLM)的广泛应用,开发者常面临多个挑战: 各大模型(OpenAI、Claude、Gemini、Ollama)接口风格不统一;缺乏一个统一平台进行模型调用与测试;本地模型 Ollama 的集成与前…...

九天毕昇深度学习平台 | 如何安装库?

pip install 库名 -i https://pypi.tuna.tsinghua.edu.cn/simple --user 举个例子&#xff1a; 报错 ModuleNotFoundError: No module named torch 那么我需要安装 torch pip install torch -i https://pypi.tuna.tsinghua.edu.cn/simple --user pip install 库名&#x…...