qml 之item学习

本文主要探讨qml中Item的基础属性,包括其作为所有基本组件基类的角色,以及如何控制Item的位置。通过实例展示了动态创建和销毁Item的操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

qml之动态创建与销毁

qml 之item学习

Item是所有基本组件的基类,本篇主要介绍Item一些基本属性,及Item位置控制。

  //item:基本的控件都继承自Item,例如Rectangle、Flickable等等
    Item{
        //相当于实例化的类对象
        id:item
        //visible:控制tem的显示隐藏
        visible: true
        //width: height 控制item的大小
        width:300
        height:200
        //opacity item的透明度
        opacity: 1
        //anchors.centerIn:控制item的位置(居中)
        anchors.centerIn: parent
        Rectangle{
            id:item1
            width:150
            height:100
            //item1的颜色
            color:"#000000"
            Text{
                id:textValue
                //color:设置文本颜色
                color:"green"
                anchors.fill: parent
                text:qsTr("learn qml...----/////省略")
                //font.pixelSize:文本大小
                font.pixelSize: 14
                //elide:富文本缩略格式
                elide: Text.ElideRight
            }
        }
        Rectangle{
            id:item2
            width:150
            height:100
            color:"blue"
            //x y:设置item2相对于父组件(item)的位置
            x:150
            y:0
        }
        Rectangle{
            id:item3
            width:150
            height:100
            color:"red"
            x:0
            y:100
        }
        Rectangle{
            id:item4
            width:150
            height:100
            color:"#ffffff"
            anchors{
                top:item1.bottom
                topMargin: 0
                left:item1.right
                leftMargin: 0
            }
        }
    }

运行结果:
![这里写图片描述](https://img-blog.youkuaiyun.com/20170405104756522?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvYmFpZHVfMTUxNzM5NTk=/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值