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

对的,这里就是说上面的那个红色的按钮,这里需要了解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. 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 ...

  2. 学习openGL-windows环境配置

    windows对openGL的支持直到1.1,而如今openGL版本已经更新到4.5,为了使用高版本的API,需要安装拓展库(glew). openGL只是个渲染系统,但是它不能产生窗口,需要依赖其它 ...

  3. mybatis映射文件参数处理 #{}取值与${}取值的区别

    #{}:是以预编译的映射,将参数设置到sql语句中,和jdbc的preraredStatement一样,使用占位符,防止sql注入. ${}:取出的值会直接拼装在sql中,会有安全问题. 大多数情况下 ...

  4. Xcode-push到远程仓库不能使用邮箱名,需使用昵称

    1.Xcode-push到远程仓库不能使用邮箱 2.Xcode-push到远程仓库需使用昵称

  5. Vue01 vue基础、mvvm、ES6z知识点、计算属性、生命周期

    Vue案例: <body> <div id="app"> <!--第一部分--> <fieldset> <legend> ...

  6. css清除间隙

    .clear{clear:both;height:0;width:0;line-height:0;overflow:hidden;}

  7. Oracle中按规定的字符截取字符串

    CREATE OR REPLACE FUNCTION "F_SPLIT" (p_str IN CLOB, p_delimiter IN VARCHAR2) RETURN ty_st ...

  8. iOS读取info.plist中的值

    在app运行的时候,需要读取info.plist中的信息,用以下代码可获取整个info.plist的信息 [[NSBundle mainBundle] infoDictionary] 以下为根据 ke ...

  9. linux系统中用户

    一.用户身份介绍: 1.系统管理员用户,UID(User IDentification) :0, 2.系统用户,UID为1~999,默认的程序都有独立的系统用户负责,运行,进而控制被破坏的范围, 3. ...

  10. Android小例子:使用反射机制来读取图片制作一个图片浏览器

    效果图: 工程文件夹: 该例子可供于新手参考练习,如果有哪里不对的地方,望指正>-< <黑幕下的人> java代码(MainActivity.java): package co ...