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;

        }

    }

}

  

qml : qml控件自适应;的更多相关文章

  1. 【IOS界面布局】横竖屏切换和控件自适应(推荐)

    [IOS界面布局]横竖屏切换和控件自适应(推荐) 分类: [MAC/IOS下开发]2013-11-06 15:14 8798人阅读 评论(0) 收藏 举报 横竖屏切换 自适应 第一种:通过人为的办法改 ...

  2. C# WinForm窗体及其控件自适应各种屏幕分辨率

    C# WinForm窗体及其控件自适应各种屏幕分辨率 一.说明  我们自己编写程序的界面,会遇到各种屏幕分辨率,只有自适应才能显的美观.实际上,做到这点也很简单,就是首先记录窗体和它上面控件的初始位置 ...

  3. C# Winform窗体和控件自适应大小

    1.在项目中创建类AutoSizeForm AutoSizeForm.cs文件代码: using System; using System.Collections.Generic; using Sys ...

  4. QML常用控件

    这里的控件是显示的元素 1.Item:一切的基类 Item { Image { source: "tile.png" } Image { x: width: height: sou ...

  5. Winfon 页签切换及窗体控件自适应

    由于公司的业务调整,最近不仅开发bs,还有不熟悉的cs,人手也不足,项目还多,对于cs来说,算是小白,虽然是一个人,也是硬着头皮写,拖拽控件,自定义控件.一个项目下来,对cs有了很深的认识,这里好好感 ...

  6. Winform窗体控件自适应大小

    自己写的winform窗体自适应大小代码,代码比较独立,很适合贴来贴去不会对原有程序造成影响,可以直接继承此类或者把代码复制到自己的代码里面直接使用 借鉴了网上的一些资料,最后采用重写WndProc方 ...

  7. C#(winform)实现不同DPI控件自适应1

    1. PicBox控件 顺便一提关键字:stretch [stretʃ] vt. 伸展,张开 //控制pictureBox图片的显示格式 this.picClose.BackgroundImageLa ...

  8. MFC的停靠窗口中插入对话框,在对话框中添加控件并做控件自适应

    单文档程序添加了停靠窗口后,可能会在停靠窗口中添加一些控件.在这里我的做法是在对话框上添加控件并布局,然后将这个对话框插入到停靠窗口中. 步骤 1.插入对话框,在对话框中放入控件(我的为树形控件),并 ...

  9. 【2017-12-06】winfrom 窗体自启最大化,控件自适应

    先将窗体windowstate属性设置为Maximized public partial class Form1 : Form { public Form1() { InitializeCompone ...

随机推荐

  1. C++17剖析:string_view的实现,以及性能

    主要内容 C++17标准发布,string_view是标准新增的内容.这篇文章主要分析string_view的适用范围.注意事项,并分析string_view带来的性能提升,最后从gcc 8.2的li ...

  2. linux备份还原命令

    使用范围:1.可以作为系统还原点,还原到备份时的状态 2.系统完全损坏后无法启动,通过引导盘恢复 一.备份还原系统命令 方法一: 备份:tar cvpzf backup.tgz --exclude=/ ...

  3. c/c++ linux 进程 fork wait函数

    linux 进程 fork wait函数 fork:创建子进程 wait:父进程等待子进程结束,并销毁子进程,如果父进程不调用wait函数,子进程就会一直留在linux内核中,变成了僵尸进程. for ...

  4. Python爬虫之Requests库的基本使用

    import requests response = requests.get('http://www.baidu.com/') print(type(response)) print(respons ...

  5. LeetCode算法题-Reverse Words in a String III(Java实现)

    这是悦乐书的第259次更新,第272篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第126题(顺位题号是557).给定一个字符串,您需要反转句子中每个单词中的字符顺序,同 ...

  6. Docker: 企业级镜像仓库Harbor的使用

    上一节,演示了Harbor的安装部署 这次我们来讲解 Harbor的使用. 我们需要了解到: 1. 如何推镜像到镜像仓库 2. 如何从镜像仓库拉取镜像 3. 如何运行从私有仓库拉取的镜像 # 查看 h ...

  7. TypeScript——初步认识

    JavaScript的痛点 众所周知,前端开发“三剑客”分别是HTML.CSS以及JS,其中JS由于其灵活简单等特点获得了大家的青睐,但是成也萧何败萧何,由于JS设计初衷是浏览器的嵌入式脚本语言,作为 ...

  8. IDF-cookie欺骗

    原题链接:http://ctf.idf.cn/game/web/40/index.php 进入题目,发现一个长字符串,放到md5.base64均无意义. 观察地址栏,发现有两个参数,line和file ...

  9. 英语口语练习系列-C06-购物

    <水调歌头>·苏轼 明月几时有,把酒问青天. 不知天上宫阙,今夕是何年? 我欲乘风归去,又恐琼楼玉宇, 高处不胜寒. 起舞弄清影,何似在人间! 转朱阁,低绮户,照无眠. 不应有恨,何事长向 ...

  10. jpa 联合查询方法

    public interface TaskBaseline { String getNumber(); String getTitle(); String getName(); String getP ...