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. ssh服务突然连接不了案例总结

    ssh服务突然连接不了案例总结   一台Oracle数据库服务器(Linux版本为Oracle Linux Server release 5.7)今天中午突然出现短暂的ssh连接不上的情况,ssh连接 ...

  2. Linux 下 C# Mono P/Invoke .so 动态链接库。

    linux 的动态链接库 libgw.so 的函数 如下: struct lbt_chan { uint32_t freq_hz; uint16_t scan_time_us; }; struct l ...

  3. 本地系统服务例程:Nt和Zw系列函数

    Windows本地操作系统服务API由一系列以Nt或Zw为前缀的函数实现的,这些函数以内核模式运行,内核驱动可以直接调用这些函数,而用户层程序只能通过系统进行调用.通常情况下用户层应用程序不会直接调用 ...

  4. Servlet是否单例?

    1,测试环境: Java SE版本:1.8.0_161(AMD64) Tomcat版本:9.0.7(AMD64) 2,试验 (1)编写HelloServlet. 由于测试代码很简单,此处只列出doGe ...

  5. CentOS7编译安装php7.1

    1.首先安装依赖包: yum install libxml2 libxml2-devel openssl openssl-devel bzip2 bzip2-devel libcurl libcurl ...

  6. 【Python 05】Python开发环境搭建

    Python3安装和使用 1.安装 Python管方下载地址 选择Customize installation安装,并且勾选Add Python 3.X to PATH. 勾选Documentatio ...

  7. .NET CORE学习笔记系列(2)——依赖注入[7]: .NET Core DI框架[服务注册]

    原文https://www.cnblogs.com/artech/p/net-core-di-07.html 包含服务注册信息的IServiceCollection对象最终被用来创建作为DI容器的IS ...

  8. Linux Swap交换分区探讨

    Swap交换分区概念 Linux divides its physical RAM (random access memory) into chucks of memory called pages. ...

  9. poj 2255 Tree Recovery(求后序遍历,二叉树)

    版权声明:本文为博主原创文章,未经博主同意不得转载.vasttian https://blog.csdn.net/u012860063/article/details/37699219 转载请注明出处 ...

  10. 25 python 初学(socket,socketserver)

    参考blog :www.cnblogs.com/yuanchenqi/articles/5692716.html 1. sk = socket.socket() 里面有两个重要的参数,family 和 ...