Qt教程之利用Qml实现滑动按钮效果

0 773
索鸟 2021-03-16
需要:0索币


整理成了一个自定义组件方便调用,下面是完整代码:
import QtQuick 2.0

Rectangle{
    id:outerRec
    radius:25;
    implicitWidth: 100;
    implicitHeight: 50;
    color:initOuterColor;
    property real count: 0
    property color initOuterColor: "lightgrey"
    property color aferChangedColor: "#8a2be2"
    property alias innerColor: innerRec.color;
    Rectangle{
        id:innerRec
        anchors.left: parent.left;
        anchors.leftMargin: 2;
        anchors.bottom: parent.bottom;
        anchors.bottomMargin: 2;
        height:46;
        width:49;
        radius:25;
        color:"white";

        states:[
            State{
                name:"left";
                when:mousearea.pressed&count%2==0
                changes: [
                    PropertyChanges{
                        target:innerRec;
                        anchors.leftMargin: 1;
                        anchors.rightMargin: 50;
                        restoreEntryValues: false;
                    }
                ]
            },
            State{
                name:"right";
                when:mousearea.pressed&count%2==1
                changes:[
                    PropertyChanges{
                        target:innerRec;
                        anchors.leftMargin: 50;
                        anchors.rightMargin: 1;
                        restoreEntryValues: false;

                    }
                ]
            }
        ]
    }
    MouseArea{
        id:mousearea
        anchors.fill:parent;
        onPressed: {
            count++;
            if(count%2==0)
                parent.color=initOuterColor;
            else
                parent.color=aferChangedColor;
        }
    }
}

感觉还可以加动画,特效之类的.

回帖
  • 消灭零回复
相关主题
2020年最新最新Kubernetes视频教程(K8s)教程 2
程序员转型之制作网课变现,月入过万告别996 1
索鸟快传2.0发布啦 1
两个不同网络的电脑怎么实现文件的互相访问呢? 1
网盘多账号登录软件 1
Java实战闲云旅游项目基于vue+element-ui 1
单点登录技术解决方案基于OAuth2.0的网关鉴权RSA算法生成令牌 1
QT5获取剪贴板上文本信息QT设置剪贴板内容 1
springboot2实战在线购物系统电商系统 1
python web实战之爱家租房项目 1
windows COM实用入门教程 1
C++游戏开发之C++实现的水果忍者游戏 1
计算机视觉库opencv教程 1
node.js实战图书管理系统express框架实现 1
C++实战教程之远程桌面远程控制实战 1
相关主题
PHP7报A non well formed numeric value encountered 0
Linux系统下关闭mongodb的几种命令分享 0
mongodb删除数据、删除集合、删除数据库的命令 0
Git&Github极速入门与攻坚实战课程 0
python爬虫教程使用Django和scrapy实现 0
libnetsnmpmibs.so.31: cannot open shared object file 0
数据结构和算法视频教程 0
redis的hash结构怎么删除数据呢? 0
C++和LUA解析器的数据交互实战视频 0
mongodb errmsg" : "too many users are authenticated 0
C++基础入门视频教程 0
用30个小时精通C++视频教程可能吗? 0
C++分布式多线程游戏服务器开发视频教程socket tcp boost库 0
C++培训教程就业班教程 0
layui的util工具格式时间戳为字符串 0
C++实战教程之远程桌面远程控制实战 1
网络安全培训视频教程 0
LINUX_C++软件工程师视频教程高级项目实战 0
C++高级数据结构与算法视频教程 0
跨域问题很头疼?通过配置nginx轻松解决ajax跨域问题 0