QML 自定义矩形框Rectangle,实现四个边框自定义大小

一、自定义矩形

效果图

边框大小为:左2 上2 右5 下10

简单来说,就是定义两个矩形,一个在外边一个在内部;

再通过设置他们的边距,即可设置相应的边框宽度;

1.编码

新建空的qml文件

MyRectangle.qml

import QtQuick 2.0

Rectangle {
    id: borderRect
    property int innerTopMargin: 0        // 上边距
    property int innerBottomMargin: 0     // 下边距
    property int innerLeftMargin: 0       // 左边距
    property int innerRightMargin: 0      // 右边距
    property string innerColor: "white"   // 矩形颜色
    property string bodercolor: "black"   // 边框颜色

    width: 100
    height: 50
    color: innerColor

    Rectangle {
        id: innerRect
        color: bodercolor

        anchors.fill: parent    // 填充满父类
        anchors.topMargin: innerTopMargin
        anchors.bottomMargin: innerBottomMargin
        anchors.leftMargin: innerLeftMargin
        anchors.rightMargin: innerRightMargin
    }
}

2.使用

// 自定义矩形                                                 
MyRectangle {                                               
    height: 100                                             
    width: 200                                              
                                                            
    innerTopMargin: 2       // 顶部边框大小                       
    innerBottomMargin: 10   // 底部                           
    innerLeftMargin: 2      // 左边                           
    innerRightMargin: 5     // 右边                           
                                                            
    bodercolor: "black"    // 边框颜色                         
    innerColor: "yellow"     // 矩形颜色                          
                                                            
    // 居中                                                   
    anchors.verticalCenter: parent.verticalCenter           
    anchors.horizontalCenter: parent.horizontalCenter       
    //anchors.centerIn: parent                              
}                                                  

二、九宫格

可以使用矩形的相对位置,去设置一个九宫格出来;

即 以下这四个属性

anchors.top        anchors.left        anchors.right        anchors.bottom

源码如下:

import QtQuick 2.9
import QtQuick.Window 2.2

import QtQuick.Controls 2.2

Window {
    visible: true
    width: 640
    height: 480
    title: qsTr("Hello World")
    color: "white"


    MyRectangle {
        id: rect1
        x: 50
        y: 50
        height: 50
        width: 100

        innerTopMargin: 2       // 顶部边框大小
        innerBottomMargin: 10   // 底部
        innerLeftMargin: 2      // 左边
        innerRightMargin: 5     // 右边

        bodercolor: "black"    // 边框颜色
        innerColor: "yellow"     // 矩形颜色
    }

    MyRectangle {
        id: rect2
        y: rect1.y
        width: rect1.width
        height: rect1.height

        innerTopMargin: rect1.innerTopMargin
        innerBottomMargin: rect1.innerBottomMargin
        innerLeftMargin: rect1.innerLeftMargin
        innerRightMargin: rect1.innerRightMargin

        bodercolor: rect1.bodercolor
        innerColor: rect1.innerColor

        anchors.left: rect1.right
        anchors.leftMargin: 5
    }

    MyRectangle {
        id: rect3
        y: rect1.y
        width: rect1.width
        height: rect1.height

        innerTopMargin: rect1.innerTopMargin
        innerBottomMargin: rect1.innerBottomMargin
        innerLeftMargin: rect1.innerLeftMargin
        innerRightMargin: rect1.innerRightMargin

        bodercolor: rect1.bodercolor
        innerColor: rect1.innerColor

        anchors.left: rect2.right
        anchors.leftMargin: 5
    }

    MyRectangle {
        id: rect4
        x: rect1.x
        y: rect1.y
        width: rect1.width
        height: rect1.height

        innerTopMargin: rect1.innerTopMargin
        innerBottomMargin: rect1.innerBottomMargin
        innerLeftMargin: rect1.innerLeftMargin
        innerRightMargin: rect1.innerRightMargin

        bodercolor: rect1.bodercolor
        innerColor: rect1.innerColor

        anchors.top: rect1.bottom
        anchors.topMargin: 5
    }

    MyRectangle {
        id: rect5
        width: rect1.width
        height: rect1.height

        innerTopMargin: rect1.innerTopMargin
        innerBottomMargin: rect1.innerBottomMargin
        innerLeftMargin: rect1.innerLeftMargin
        innerRightMargin: rect1.innerRightMargin

        bodercolor: rect1.bodercolor
        innerColor: rect1.innerColor

        anchors.top: rect2.bottom
        anchors.left: rect4.right
        anchors.leftMargin: 5
        anchors.topMargin: 5
    }


    MyRectangle {
        id: rect6
        width: rect1.width
        height: rect1.height

        innerTopMargin: rect1.innerTopMargin
        innerBottomMargin: rect1.innerBottomMargin
        innerLeftMargin: rect1.innerLeftMargin
        innerRightMargin: rect1.innerRightMargin

        bodercolor: rect1.bodercolor
        innerColor: rect1.innerColor

        anchors.top: rect3.bottom
        anchors.left: rect5.right
        anchors.leftMargin: 5
        anchors.topMargin: 5
    }

    MyRectangle {
        id: rect7
        x: rect1.x
        y: rect1.y
        width: rect1.width
        height: rect1.height

        innerTopMargin: rect1.innerTopMargin
        innerBottomMargin: rect1.innerBottomMargin
        innerLeftMargin: rect1.innerLeftMargin
        innerRightMargin: rect1.innerRightMargin

        bodercolor: rect1.bodercolor
        innerColor: rect1.innerColor

        anchors.top: rect4.bottom
        anchors.topMargin: 5
    }

    MyRectangle {
        id: rect8
        width: rect1.width
        height: rect1.height

        innerTopMargin: rect1.innerTopMargin
        innerBottomMargin: rect1.innerBottomMargin
        innerLeftMargin: rect1.innerLeftMargin
        innerRightMargin: rect1.innerRightMargin

        bodercolor: rect1.bodercolor
        innerColor: rect1.innerColor

        anchors.top: rect5.bottom
        anchors.left: rect7.right
        anchors.leftMargin: 5
        anchors.topMargin: 5
    }

    MyRectangle {
        id: rect9
        width: rect1.width
        height: rect1.height

        innerTopMargin: rect1.innerTopMargin
        innerBottomMargin: rect1.innerBottomMargin
        innerLeftMargin: rect1.innerLeftMargin
        innerRightMargin: rect1.innerRightMargin

        bodercolor: rect1.bodercolor
        innerColor: rect1.innerColor

        anchors.top: rect6.bottom
        anchors.left: rect8.right
        anchors.leftMargin: 5
        anchors.topMargin: 5
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

cpp_learners

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值