项目基本信息前两个已经说了,这里直接放下运行截图,

对的,这里就是说上面的那个红色的按钮,这里需要了解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-自定义一个按钮的更多相关文章

  1. Qt-第一个QML程序-4-自定义按钮类,动画,状态

    上篇中,我们写到了自己定义了一个按钮,但是呢,按照这样的写法,要写一个程序出来,那要累死了,所以,qml给我的感觉就是各种随便调用,所以了,可以自己写一个自己Button的qml,这样在以后用到了,就 ...

  2. Qt-第一个QML程序-1-项目工程的建立

    这个小程序是我发的第一个完整的QMl程序,这个程序也会持续的更新,一步一步的完善起来,最后会有一个什么样的结果也是不知道,只是把自己目前掌握的QML相关的东西都慢慢的写进来,积累起来 先展示一下运行结 ...

  3. Qt-第一个QML程序-2-关键代码分析,TEXT,Image,Mouseare

    qml语言开始写的时候有点不习惯,后面用的多了感觉很好,很顺手,用于快速搭建项目界面,真的很好. 目前用到的还是比较简单的 隐藏标题栏,而依附任务栏 flags: Qt.Window | Qt.Fra ...

  4. Android学习笔记_70_一个应用程序启动另一个应用程序的Activity

    第一种(我自己写的) :之前在网上看来一些,很多不是我要的可以启动另外一个应用程序的主Activity. //这些代码是启动另外的一个应用程序的主Activity,当然也可以启动任意一个Activit ...

  5. windows phone 8 新增功能:从一个应用程序启动另一个程序(file association 和 Protocol association两种方式)

    一. 启动手机预装内置程序打开文件file association 这里以打开word文档为例子 string fileToLaunch = @"HelloKitty.docx"; ...

  6. 第一个lucene程序,把一个信息写入到索引库中、根据关键词把对象从索引库中提取出来、lucene读写过程分析

    新建一个Java Project :LuceneTest 准备lucene的jar包,要加入的jar包至少有: 1)lucene-core-3.1.0.jar     (核心包) 2) lucene- ...

  7. C#像运行一个exe 程序一样运行一个dll文件

    [DllImport("kernel32.dll")] public static extern int WinExec(string exeName, int operType) ...

  8. 第一个PyQt程序

    这个程序虽然小,具备pyqt程序的皱型,可以作为一个模板使用了 #!/usr/bin/python3 # -*- coding: utf-8 -*- import sys from PyQt5.QtW ...

  9. C#特性:从自定义一个特性开始,谈谈什么是特性

    作为C#新手中的一员,我刚开始接触特性时,那真是一脸冏逼啊,怎么想怎么查资料都没弄明白它到底是个什么东西,有的入门教程甚至都没讲特性和反射这些概念!相信很多人第一次接触到特性就是关于系列化的知识了. ...

随机推荐

  1. BZOJ 3744: Gty的妹子序列 【分块 + 树状数组 + 主席树】

    任意门:https://www.lydsy.com/JudgeOnline/problem.php?id=3744 3744: Gty的妹子序列 Time Limit: 20 Sec  Memory ...

  2. Tree - Rooted Trees

    Rooted Trees A graph G = (V, E) is a data structure where V is a finite set of vertices and E is a b ...

  3. npm run build 打包后,如何运行在本地查看效果

    目前,使用vue-cli脚手架写了一个前端项目,之前一直是使用npm run dev 在8080端口上进行本地调试.项目已经进行一半了,今天有时间突然想使用npm run build进行上线打包,试试 ...

  4. 使用GraphViz画caffe网络结构图

    参考http://blog.csdn.net/happynear/article/details/45440709 1. 安装pydot: sudo pip install pydot 2. 安装Gr ...

  5. ringMVC——redirect重定向跳转传值

    spring MVC框架controller间跳转,需重定向.有几种情况:不带参数跳转,带参数拼接url形式跳转,带参数不拼接参数跳转,页面也能显示.     首先先来介绍一下不带参数的重定向:    ...

  6. 如何在IAR工程中创建和使用模板

    路径为:Edit -> Code Templates -> Edit Templates  如下图: #TEMPLATE "&FileDeclare>&Fi ...

  7. etcd部署说明

    etcd是一个K/V分布式存储,每个节点都保存完成的一份数据.有点类似redis.但是etcd不是数据库. 1.先说废话.之所以会用etcd,并不是实际项目需要,而是前面自己写的上传的DBCacheS ...

  8. JAVA交通规则

    第一个JAVA程序的编写和运行 1.使用记事本编辑: public class Welcome { public static void main(String[] args) { System.ou ...

  9. 2018 kali linux install tools

    1.VM setup https://www.vmware.com/products/workstation-pro/workstation-pro-evaluation.html VMware-Wo ...

  10. MFC+ODBC+SQL Server+Visual C++

    利用SQL Server 和MFC实现对数据库的简单管理 工具:SQL Server,VC6.0 步骤如下: 1.建立一个数据库studentinfo,再建立一个表testtable,表设计和初始值如 ...