P2-QML-Item与Rectangle

Item的属性

设置z来控制控件显示

在这里插入图片描述

接收键盘信息(回车键)

在这里插入图片描述

填充父控件

在这里插入图片描述

设置控件之间的距离

Window {
    visible: true              // 是否隐藏
    width: 640                 // 宽度
    height: 480                // 高度
    title: qsTr("Hello World") // 设置应用程序名称

    /* 设置控件之间的距离 */
    Rectangle{
        id: rect1
        width: 50;
        height: 50;
        color: "black"
    }

    Rectangle{
        id: rect2
        width: 50;
        height: 50;
        anchors.left: rect1.right
        anchors.leftMargin: 20    //rect2在rect1右边的20个单位
        color: "black"
    }

    Rectangle{
        id: rect3
        width: 50;
        height: 50;
        anchors.top: rect1.bottom
        anchors.topMargin: 20    //rect2在rect1右边的20个单位
        color: "black"
    }
}

在这里插入图片描述

相对于父控件的位置

在这里插入图片描述

控件的旋转和放缩

在这里插入图片描述

Rectangle属性

Rectangle的边框宽度和颜色,边框圆角

在这里插入图片描述

全部代码

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") // 设置应用程序名称

    /* Item属性 */


    /* 可以设置z来控制控件显示层级 */
    Rectangle{
        x: 100;
        y: 100;
        z: 1        // 这个是调节页面显示的优先级,默认为0
        width: 100;
        height: 50;
        color: "black";
    }

    Rectangle{
        x: 120;
        y: 120;
        width: 100;
        height: 50;
        color: "red"
    }


    /* 如果我们的控件想接收键盘信息的时候,我们就需要先获取到焦点 */
    Rectangle{
        x: 200;
        y: 200;
        width: 100;
        height: 50;
        color: "blue";
        focus: true

        MouseArea{
            anchors.fill: parent
            onClicked: {
                console.log("on clicked")
            }
        }

        Keys.onReturnPressed: {
            console.log("on return pressed")
        }
    }

    /* 填充父控件 */
    Rectangle{
        anchors.fill: parent // 整个窗口被这个控件填充
        color: "blue";
    }


    /* 设置控件之间的距离 */
    Rectangle{
        id: rect1
        width: 50;
        height: 50;
        color: "black"
    }

    Rectangle{
        id: rect2
        width: 50;
        height: 50;
        anchors.left: rect1.right
        anchors.leftMargin: 20    //rect2在rect1右边的20个单位
        color: "black"
    }

    Rectangle{
        id: rect3
        width: 50;
        height: 50;
        anchors.top: rect1.bottom
        anchors.topMargin: 20    //rect2在rect1右边的20个单位
        color: "black"
    }

    /* 相对于父控件的位置 */
    Rectangle{
        id: rect4
        width: 50;
        height: 50;
        color: "black"
        anchors.horizontalCenter: parent.horizontalCenter  // 控件在父窗口顶部居中
        anchors.centerIn: parent// 父窗口中心居中
    }

    /* 控件的旋转和放缩 */
    Rectangle{
        id: rect5
        width: 50
        height: 50;
        scale: 2         // 放大两倍
        rotation: 30     // 旋转30°
        color: "black"
        anchors.centerIn: parent
    }


    /* Rectangle属性 */

    /* Rectangle的边框宽度和颜色,边框圆角 */
    Rectangle{
        id: rect6
        width: 50
        height: 50;
        color: "black"
        radius: 15 // 圆角15
        border.width: 2;
        border.color: "yellow"  // 边框宽度为2,颜色为黄色
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值