• qml : qml控件自适应;


    import QtQuick 2.4
    
     
    
    Item {
    
        property var targetItem: parent
    
        property bool fixedAspectRatio: true // Else zoom from width and height
    
        property bool accordingToX: true // Else according to center
    
     
    
        property var targetItemGeometry
    
        property var childrenItemGeometry
    
     
    
        property bool isBegin: false
    
     
    
        function begin() {
    
            targetItemGeometry = new Object;
    
            targetItemGeometry["width"] = targetItem.width;
    
            targetItemGeometry["height"] = targetItem.height;
    
     
    
            var children = targetItem.children;
    
            var data = new Array;
    
            for(var index = 1; index < children.length; index++)
    
            {
    
                var currentItem = children[index];
    
                var buf = new Object;
    
     
    
                buf["item"] = currentItem;
    
                buf["x"] = currentItem.x;
    
                buf["y"] = currentItem.y;
    
                buf["centerX"] = currentItem.x + (currentItem.width / 2);
    
                buf["centerY"] = currentItem.y + (currentItem.height / 2);
    
                buf["width"] = currentItem.width;
    
                buf["height"] = currentItem.height;
    
     
    
                data.push(buf);
    
            }
    
     
    
            childrenItemGeometry = data;
    
            isBegin = true;
    
        }
    
     
    
        function resize() {
    
            if(isBegin)
    
            {
    
                var horizontalRatio, verticalRatio;
    
     
    
                horizontalRatio = targetItem.width / targetItemGeometry["width"];
    
                verticalRatio = targetItem.height / targetItemGeometry["height"];
    
     
    
                for(var index = 0; index < childrenItemGeometry.length; index++)
    
                {
    
                    if(fixedAspectRatio)
    
                    {
    
                        if(horizontalRatio > verticalRatio)
    
                        {
    
                            childrenItemGeometry[index]["item"].width  = childrenItemGeometry[index]["width"] * verticalRatio;
    
                            childrenItemGeometry[index]["item"].height = childrenItemGeometry[index]["height"] * verticalRatio;
    
                        }
    
                        else
    
                        {
    
                            childrenItemGeometry[index]["item"].width  = childrenItemGeometry[index]["width"] * horizontalRatio;
    
                            childrenItemGeometry[index]["item"].height = childrenItemGeometry[index]["height"] * horizontalRatio;
    
                        }
    
                    }
    
                    else
    
                    {
    
                        childrenItemGeometry[index]["item"].width  = childrenItemGeometry[index]["width"] * horizontalRatio;
    
                        childrenItemGeometry[index]["item"].height = childrenItemGeometry[index]["height"] * verticalRatio;
    
                    }
    
     
    
                    if(accordingToX)
    
                    {
    
                        childrenItemGeometry[index]["item"].x = childrenItemGeometry[index]["x"] * horizontalRatio;
    
                        childrenItemGeometry[index]["item"].y = childrenItemGeometry[index]["y"] * verticalRatio;
    
                    }
    
                    else
    
                    {
    
                        childrenItemGeometry[index]["item"].x = childrenItemGeometry[index]["centerX"] * horizontalRatio - (childrenItemGeometry[index]["item"].width / 2);
    
                        childrenItemGeometry[index]["item"].y = childrenItemGeometry[index]["centerY"] * verticalRatio - (childrenItemGeometry[index]["item"].height / 2);
    
                    }
    
                }
    
            }
    
        }
    
     
    
        Component.onCompleted: {
    
            begin();
    
        }
    
     
    
        Component {
    
            id: connections
    
     
    
            Connections {
    
                target: targetItem
    
     
    
                onWidthChanged: {
    
                    resize();
    
                }
    
                onHeightChanged:
    
                {
    
                    resize();
    
                }
    
            }
    
        }
    
     
    
        Loader {
    
            Component.onCompleted: {
    
                sourceComponent = connections;
    
            }
    
        }
    
    }
    

      

  • 相关阅读:
    Java 8 stream的详细用法
    SpringBoot启动异常 Process finished with exit code 1
    GIT-版本管理-初阶使用
    升级 kubeadm 集群
    antdv 获取 axios文件上传实时进度
    Ant Design Vue 实现文件上传 (通过点击提交按钮后开始上传)
    Ant Design Vue 实现菜单栏根据url变化自动高亮和展开
    Do not access Object.prototype method 'hasOwnProperty' from target object
    Nginx配置WebSocket (包含nginx-ingress-controller)
    Django ORM 常用字段和参数/关系字段/ForeignKey操作/数据库查询优化(重要)/事务初识
  • 原文地址:https://www.cnblogs.com/yinwei-space/p/10168393.html
Copyright © 2020-2023  润新知