QT QML控件自动缩放

前言:

转载请附上连接,本帖原创请勿照抄。

效果图:

QML部分:

main.qml

import QtQuick 2.12
import QtQuick.Window 2.12
import QtQuick.Controls 2.5

Window {
    visible: true
    width: 640
    height: 480
    title: qsTr("AutoResize")

    id:window
    //变量的定义
    property int num : 1

    Rectangle {
        width: parent.width / 2
        height: parent.height / 2
        color: "#00000000"



        AutoResize {
            fixedAspectRatio: true
            accordingToX: true
        }
        Button{
            x:100  //设置按钮的横坐标
            y:100  //设置纵坐标
            text:"我是按钮"   //按钮标题

            //信号槽连接,单击信号
            onClicked: {
                console.log("我被点击了,输出变量num = " + num)
            }
            //双击信号
    //        onDoubleClicked: {
    //            slotDouble();
    //        }
            }

        ComboBox {
                    id:combox
                    x:250  //设置按钮的横坐标
                    y:250  //设置纵坐标
                    currentIndex: 0
                    model: ListModel {
                        id: cbItems
                        ListElement { text: "当前节点前"; color: "Yellow" }
                        ListElement { text: "当前节点后"; color: "Green" }
                    }
                    width: 160
                    onCurrentIndexChanged: {
                        if(currentIndex==0){
                            console.debug(cbItems.get(currentIndex).text + ", " + cbItems.get(currentIndex).color)
                            console.debug("0")
                        }
                        if(currentIndex==1){
                             console.debug("1")
                        }
                    }
                }
        }


}

AutoResize.qml

import QtQuick 2.0

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;
        }
    }
}

完整Demo下载:https://mp.csdn.net/console/upDetailed

猜你喜欢

转载自blog.csdn.net/qq_37529913/article/details/110327873
今日推荐