Qt-第一个QML程序-3-自定义一个按钮
项目基本信息前两个已经说了,这里直接放下运行截图,
对的,这里就是说上面的那个红色的按钮,这里需要了解Qml的动画和状态
这里先把整个按钮的代码写出来,一点一点写
Rectangle
{
id:close_Btn
width: 50
height: 30
color: "transparent"
state: "nomal"
anchors.top: parent.top
anchors.right: parent.right
Text {
id: close_Text
anchors.centerIn: parent
text: qsTr("X")
}
MouseArea
{
id:close_MouseArea
hoverEnabled: true
anchors.fill: parent
onClicked:
{
close_Btn.state="press"
Qt.quit()
}
onEntered: close_Btn.state="hover"
onExited: close_Btn.state="nomal"
}
states:
[
State
{
name: "nomal"
PropertyChanges
{
target: close_Btn
color:"#777777"
}
},
State
{
name: "hover"
PropertyChanges
{
target: close_Btn
color:"red"
}
},
State
{
name: "press"
PropertyChanges
{
target: close_Btn
color:"blue"
}
}
]
transitions:
[
Transition
{
from: "nomal"
to: "hover" ColorAnimation
{
target: close_Btn
duration: 100
}
},
Transition
{
from: "hover"
to: "press" ColorAnimation
{
target: close_Btn
duration: 100
}
},
Transition
{
from: "press"
to: "nomal" ColorAnimation
{
target: close_Btn
duration: 100
}
}
] }
这是一个按钮的大整体代码,可能用过widget那边的人会觉得好多,确实是,不过后期我可能会把这个按钮封装成一个整的Button就可以了,使用起来就会简单好多
id:close_Btn
width: 50
height: 30
color: "transparent"
state: "nomal"
anchors.top: parent.top
anchors.right: parent.right
这部分就是这个按钮的基本部分,按钮大小50*30
按钮的背景颜色透明
默认状态为nomal
布局为右上角
Text {
id: close_Text
anchors.centerIn: parent
text: qsTr("X")
}
显示的文本信息,布局为在父级中心布局,文本内容“X”
MouseArea
{
id:close_MouseArea
hoverEnabled: true
anchors.fill: parent
onClicked:
{
close_Btn.state="press"
Qt.quit()
}
onEntered: close_Btn.state="hover"
onExited: close_Btn.state="nomal"
}
鼠标点击区域,把整个父级设置为鼠标点击区域,同时把鼠标滑入设置为允许。添加鼠标点击事件,当鼠标点击后,将状态改为press,同时推出程序
下面的就是当鼠标滑入是,将状态改为hover,滑出时,将状态改为nomal
states:
[
State
{
name: "nomal"
PropertyChanges
{
target: close_Btn
color:"#777777"
}
},
State
{
name: "hover"
PropertyChanges
{
target: close_Btn
color:"red"
}
},
State
{
name: "press"
PropertyChanges
{
target: close_Btn
color:"blue"
}
}
]
定义按钮的三个状态,这里就是简单的改变一下颜色
transitions:
[
Transition
{
from: "nomal"
to: "hover" ColorAnimation
{
target: close_Btn
duration: 100
}
},
Transition
{
from: "hover"
to: "press" ColorAnimation
{
target: close_Btn
duration: 100
}
},
Transition
{
from: "press"
to: "nomal" ColorAnimation
{
target: close_Btn
duration: 100
}
}
]
动画机制,需要绑定目标和时间
ok,目前完成
源码连接:http://download.csdn.net/detail/z609932088/9802022
GitHub:https://github.com/DreamLifeOffice/MyQmlProject
Qt-第一个QML程序-3-自定义一个按钮的更多相关文章
- Qt-第一个QML程序-4-自定义按钮类,动画,状态
上篇中,我们写到了自己定义了一个按钮,但是呢,按照这样的写法,要写一个程序出来,那要累死了,所以,qml给我的感觉就是各种随便调用,所以了,可以自己写一个自己Button的qml,这样在以后用到了,就 ...
- Qt-第一个QML程序-1-项目工程的建立
这个小程序是我发的第一个完整的QMl程序,这个程序也会持续的更新,一步一步的完善起来,最后会有一个什么样的结果也是不知道,只是把自己目前掌握的QML相关的东西都慢慢的写进来,积累起来 先展示一下运行结 ...
- Qt-第一个QML程序-2-关键代码分析,TEXT,Image,Mouseare
qml语言开始写的时候有点不习惯,后面用的多了感觉很好,很顺手,用于快速搭建项目界面,真的很好. 目前用到的还是比较简单的 隐藏标题栏,而依附任务栏 flags: Qt.Window | Qt.Fra ...
- Android学习笔记_70_一个应用程序启动另一个应用程序的Activity
第一种(我自己写的) :之前在网上看来一些,很多不是我要的可以启动另外一个应用程序的主Activity. //这些代码是启动另外的一个应用程序的主Activity,当然也可以启动任意一个Activit ...
- windows phone 8 新增功能:从一个应用程序启动另一个程序(file association 和 Protocol association两种方式)
一. 启动手机预装内置程序打开文件file association 这里以打开word文档为例子 string fileToLaunch = @"HelloKitty.docx"; ...
- 第一个lucene程序,把一个信息写入到索引库中、根据关键词把对象从索引库中提取出来、lucene读写过程分析
新建一个Java Project :LuceneTest 准备lucene的jar包,要加入的jar包至少有: 1)lucene-core-3.1.0.jar (核心包) 2) lucene- ...
- C#像运行一个exe 程序一样运行一个dll文件
[DllImport("kernel32.dll")] public static extern int WinExec(string exeName, int operType) ...
- 第一个PyQt程序
这个程序虽然小,具备pyqt程序的皱型,可以作为一个模板使用了 #!/usr/bin/python3 # -*- coding: utf-8 -*- import sys from PyQt5.QtW ...
- C#特性:从自定义一个特性开始,谈谈什么是特性
作为C#新手中的一员,我刚开始接触特性时,那真是一脸冏逼啊,怎么想怎么查资料都没弄明白它到底是个什么东西,有的入门教程甚至都没讲特性和反射这些概念!相信很多人第一次接触到特性就是关于系列化的知识了. ...
随机推荐
- php函数:call_user_func
前段时间浏览文档发现一个有意思的PHP函数:call_user_func [文档地址] 函数作用:该函数主要用于通过函数名去调用该函数 例如: function test(){ echo " ...
- 11572 - Unique Snowflakes(贪心,两指针滑动保存子段最大长度)
Emily the entrepreneur has a cool business idea: packaging and selling snowflakes. She has devised a ...
- HDU 1013 Digital Roots(to_string的具体运用)
传送门:http://acm.hdu.edu.cn/showproblem.php?pid=1013 Digital Roots Time Limit: 2000/1000 MS (Java/Othe ...
- win764 ping不能用的问题
1.某日发现,ping突然用不了了 2.百度一搜,有如下解答: 1.右击计算机点属性之后找到系统属性下的环境变量: 2.找到系统变量的"path"之后按“编辑”: 3.变量值前面的 ...
- Jboot使用appassembler-maven-plugin插件生成启动脚本
appassembler-maven-plugin介绍: 使用appassembler-maven-plugin插件可自动生成跨平台的启动脚本,可省去手工写脚本的麻烦,而且还可以生成jsw的后台运行程 ...
- CRT7.3.1版本安装步骤
工具: Setup.exe安装程序 keygen.exe注册机 zwt.nfo 查看电脑信息(主要看自己电脑是x86还x64版本) 安装步骤(所有程序尽量以管理员身份启动) 1.安装SecureCRT ...
- Python Monitoring UPS with SNMPWALK
##Background My co-worker told me he needed to monitor UPS with SNMP module but he only can get hexa ...
- python学习笔记(3)---cookie & session
一.cookie & session 1.cookie: cookie 就是由服务器发送给客户端的特殊信息,而这些信息以文本的方式存放在客户端,然后客户端每次向服务器发送请求都会带上这些特殊信 ...
- python3 安装pyhanlp方法
直接pip install pyhanlp的时候会提示缺少Microsoft Visual c++环境, 其实没有Microsoft Visual c++环境也是可以的, 可以先安装jpype1,然后 ...
- 20190120-自定义实现split方法
1. 实现字符串的split方法Python split() 通过指定分隔符对字符串进行切片,如果参数 num 有指定值,则分隔 num+1 个子字符串 思路同自定义实现replace方法类型: 1. ...