QT6.5+qt-quick+qml+cmake的Item布局学习

  1. Item 是一个基础元素,它本身不会渲染任何东西,也不会提供一个窗口来显示其内容。Window 是一个可以显示内容的顶级元素,它通常会包含一个或多个子元素来构建用户界面。
  2. Item是全部QML可视化对象的根,所有可视化类型都由该类型派生出来,基础的布局属性
  3. import QtQuick.Layouts 1.15-----ColumnLayout
  4. import QtQuick.Controls 2.6------Button
  5. 主要涉及内容
    1. width/height-尺寸信息-Label标签-Rectangle矩形: color:"red"Rectangle 和 Text 等元素通常支持 color 属性,但像 ColumnLayout 或 RowLayout 这样的布局元素则不支持。
    2. anchors-------相对位置
    3. x/y-------------绝对位置:以父控件的左上角为坐标原点,向右为x轴正向,向下为y轴正向
    4. 尺寸填充:fill:anchors.fill-当前的尺寸信息会变成指定Item的 尺寸信息

    5. enable----------使能,当该属性被设置为false后,该Item将不再接受任何鼠标或者键盘事件,但是在界面依然可以看到该控件

    6. visible------------可见性---界面里面的可见性

    7. z----------层级关系,数值越大,层级越高

    8. opacity-------透明度,数值越大,越不透明,有效取值范围(0.0~1.0)

  6. 下面所有的代码全在main.qml里面编写的
    import QtQuick
    import QtQuick.Layouts 1.15
    import QtQuick.Controls 2.6
    Window {
        width:300
        height:300
        visible: true//必须有这句才能显示
        title: qsTr("Hello World")
        ColumnLayout{
            Rectangle{
                width:rect2_1_0.width+rect2_1_1.width+rect2_1_2
                height:Math.max(rect2_1_0.height,rect2_1_1.height,rect2_1_2)
                Rectangle{
                    id:rect2_1_0
                    width:100
                    height:300
                    //color:"#e23333"
                    Rectangle{
                        anchors.top:parent.top
                        anchors.left: parent.left
                        anchors.topMargin: 20
                        anchors.leftMargin:20
                        width:60
                        height:60
                        color:"blue"
                        Rectangle{
                           // x:20
                            //y:20
                            //anchors.fill: parent
                            width:20
                            height:20
                            color:"red"
                            }
                        }
                    Rectangle{
                        y:120
                        x:20
                        width:60
                        height:60
                        color:"blue"
                        Rectangle{
                            x:20
                            y:20
                            width:20
                            height:20
                            color:"red"
                        }
                    }
                    Rectangle{
                        y:200
                        x:20
                        width:60
                        height:60
                        color:"blue"
                        Rectangle{
                            anchors.top:parent.top
                            anchors.left: parent.left
                            anchors.topMargin: 40
                            anchors.leftMargin:40
                            width:20
                            height:20
                            color:"red"
                        }
                    }
                }
                Rectangle{
                    id:rect2_1_1
                    width:100
                    height:300
                    color:"green"
                    anchors.left:rect2_1_0.right
                    Button{
                            x:10
                            width:80
                            height:20
                            enabled:true
                            text:"使能按键"
                        }
                    Button{
                            y:30
                            x:10
                            width:80
                            height:20
                            enabled:false
                            text:"不使能的按键"
                        }
                    Button{
                            y:60
                            x:10
                            width:80
                            height:20
                            visible:true
                            text:"可见按键"
                        }
                        Rectangle{
                            y:90
                            width:100
                            height:100
                            color:"#e23333"
                            Button{
                                y:20
                                width:100
                                height:60
                                visible:true
                                text:"不可见按键"
                            }
                        }
                    Rectangle{
                        y:220
                        x:20
                        width:60
                        height:60
                        color:"black"
                        opacity:0.5
                    }
            //它的左边锚点(`anchors.left`)被设置为`rect2_1_0.right`,意味着它将紧挨`rect2_1_0`的右侧放置,实现左右排布。
                }
                Rectangle{
                    id:rect2_1_2
                    width:100
                    height:300
                    color:"yellow"
                    anchors.left:rect2_1_1.right
                    Rectangle{
                            width:100;height:150
                            Rectangle{
                                color:"red"
                                width:75;height:75
                            }
                            Rectangle{
                                color:"blue"
                                x:25;y:25;width:75;height:75
                            }
                        }
                    //指定层级关系
                        Rectangle{
                            y:150
                            width:100;height:150
                            Rectangle{
                                z:100
                                color:"red"
                                width:75;height:75
                            }
                            Rectangle{
                                color:"blue"
                                x:25;y:25;width:75;height:75
                            }
                        }
            //它的左边锚点(`anchors.left`)被设置为`rect2_1_0.right`,意味着它将紧挨`rect2_1_0`的右侧放置,实现左右排布。
                }
            }
        }
    }
    

    效果如下:

  • 好啦,今天到这,希望大神指教!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值