Qt-第一个QML程序-3-自定义一个按钮

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

image.png

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


目录
相关文章
|
6天前
Qt控件(按钮、单选、复选、list、tree、table)
Qt控件(按钮、单选、复选、list、tree、table)
15 2
|
6天前
Qt之按钮操作
Qt之按钮操作
8 2
|
19天前
Qt MainWindow 程序主窗口
Qt MainWindow 程序主窗口
|
2月前
|
人工智能 编译器 C++
新版Qt6快速打包程序脚本
不知道啥时候Qt更新了,目前是6.7.0,项目默认的生成路径改了, 从 项目目录的同级目录 改为了 项目目录中的build目录, 之前的脚本也用不了了,所以用AI更新了一下脚本, 希望能帮到大家,方便快速打包程序到桌面。
61 9
|
2月前
|
C++
【qt】自定义代理类
【qt】自定义代理类
10 0
|
2月前
|
搜索推荐
【qt】自定义界面类
【qt】自定义界面类
17 0
|
2月前
|
开发框架 Linux C++
Qt:强大的跨平台C++应用程序开发框架
Qt:强大的跨平台C++应用程序开发框架
78 3
|
2月前
|
开发框架 编译器 C++
Qt:一个强大的跨平台C++应用程序开发框架
Qt:一个强大的跨平台C++应用程序开发框架
63 1