qml(window/Rectangle/anchors/component/loader/mousearea/button)

window 窗口类

Window {
    width:400
    height:300
    visible:true
    title:qsTr("My QML")
    property int myValue:0
    signal mySig()
    minimumHeight: 400
    minimumWidth:300
    maximumWidth: 400
    maximumHeight:300   //最大最小宽度
    onWidthChanged: {
        console.log("width:",width)
    }
    onMyValueChanged: {

    }
    onMySig:{


    }
    Button{
     id:btn1
     width:50
     height: 50
     Keys.onLeftPressed: {
        btn1.focus = true
     }
    }
}

矩形

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

    Rectangle{
        x:100
        y:100
        z:1
        width: 100
        height: 50
        color:"black"
    }

    Rectangle{
        x:150
        y:150
        width: 100
        height: 50
        focus:true//获取到焦点,可以接受键盘的信息
        color:"blue"
        MouseArea{
            anchors.fill: parent
            onClicked: {
                console.log("on clicked")
            }
        }
        Keys.onReturnPressed: {console.log("on return pressed")}

   }
}

anchors类

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

    Rectangle{
        id:rect1
        //anchors.fill:parent//填充满父类
        width:100
        height:50
        color:"black"
        border.width:2
        border.color: "red"
        anchors.leftMargin: 20
        anchors.centerIn: parent//位于父控件的中间位置
    }
}

component与loader

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

    Component{
        id:com
        Rectangle{
            width:200
            height:100
            color: "black"
            //创建的时候调用
            Component.onCompleted: {
                console.log("onCompleted",width,height,color)
            }

            //销毁的时候调用
            Component.onDestruction: {
                console.log("onCompleted",width,height,color)
            }
        }
    }

    Loader{
        id:loader
        sourceComponent: com
    }

    Button{
        width:50
        height: 50
        x:200
        onClicked: {
            loader.item.width=50//设置组件宽度
            loader.item.height = 50//设置组件高度
            //loader.sourceComponent = null//点击之后销毁
        }
    }
}

mousearea

Window {
    width:640
    height:480
    visible:true
    title:qsTr("My QML")
    color: "white"
    MouseArea{
        id:mouseArea
        width:200
        height:200
        acceptedButtons: Qt.LeftButton|Qt.RightButton
        Rectangle{
            anchors.fill:parent
            color:"black"
        }
        onClicked: {
            console.log("clicked")
        }
        onPressed: {
            console.log("pressed")
        }
    }
}

button

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

    Button{
        id:btn
        width:50
        height:50
        checkable: true //实现内置状态
        onCheckableChanged: {
            console.log("changed",checkable)
        }
        onPressedChanged: {
            btn.checked = !btn.checked
        }

        onClicked:{
            console.log("cliecked")
        }
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值