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. 【转载】xilinx 高速收发器Serdes深入研究

    此篇文章深入浅出介绍了关于高速串行收发器的几个重要概念和注意事项,为方便知识点复习总结和后续查阅特此转载,原文标题及链接为:xilinx 高速收发器Serdes深入研究 - CSDN博客   http ...

  2. Bootstrap Multiselect插件使用步骤以及常见参数配置介绍

    Multiselect是基于jQuery插件的,它可以以下拉列表的形式为用户提供选择内容,能进行单选或者多选.它应用的主要步骤如下: 一,引入需要的相关js和css文件 既然是Bootstrap插件, ...

  3. 对java中的equals()方法的总结

    Java的基础学习总结--equals方法 一,等于方法介绍 1.1.通过下面的例子掌握等于的用法 1 package cn.galc.test; 2 3 public class TestEqual ...

  4. June. 23rd 2018, Week 25th. Saturday

    We are who we choose to be. 要成为怎样的人,选择在于自己. From Barry Manilow. I believe that we are who we choose ...

  5. 力扣算法题—079单词搜索【DFS】

    给定一个二维网格和一个单词,找出该单词是否存在于网格中. 单词必须按照字母顺序,通过相邻的单元格内的字母构成,其中“相邻”单元格是那些水平相邻或垂直相邻的单元格.同一个单元格内的字母不允许被重复使用. ...

  6. 【笔记】Python集成开发环境——PyCharm 2018.3下载、注册、帮助文档

    [博客导航] [Python导航] 前言 使用好的开发环境将有效提高编程效率,在Python使用上我是小白,所以特意请教了从事语言处理的成同学,告知我,推荐使用Pycharm和IntelliJ. 目前 ...

  7. .NET CORE学习笔记系列(2)——依赖注入[5]: 创建一个简易版的DI框架[下篇]

    为了让读者朋友们能够对.NET Core DI框架的实现原理具有一个深刻而认识,我们采用与之类似的设计构架了一个名为Cat的DI框架.在上篇中我们介绍了Cat的基本编程模式,接下来我们就来聊聊Cat的 ...

  8. HBase案例:HBase 在人工智能场景的使用

    近几年来,人工智能逐渐火热起来,特别是和大数据一起结合使用.人工智能的主要场景又包括图像能力.语音能力.自然语言处理能力和用户画像能力等等.这些场景我们都需要处理海量的数据,处理完的数据一般都需要存储 ...

  9. Linux systemtap定位系统IO资源使用情况(ok)

    一.systemtap介绍 SystemTap是一个强大的调试工具,是监控和跟踪运行中的Linux 内核的操作的动态方法,确切的说应该是一门调试语言,因为它有自己的语法,也有解析.编译.运行等过程(准 ...

  10. node+mysql 数据库连接池

    1. 什么是数据库连接池? 数据库连接池是程序启动时建立足够的数据库连接,并将这些连接组成一个池,由程序动态地对池中的连接进行申请,使用和释放. 2. 使用数据库连接池原理及优点是什么? 数据库连接池 ...