【QML】Property‘s

  ​前言

学习自:B站【英文字幕】Qt quick(QML)技术解读 入门到熟练

学习性质property的一些常用的基本的用法,示例为写入按钮的Rec,Text,MouseA,和信号的property

过程

在项目添新文件AddNew,QML File,命名为MyButton,在main函数中直接写入MyButton的各种属性,不用加入Rec,MouseA和Text区域。

1.首先把MyButton的Rec和Text属性写入main函数

MyButton{
        width: 100
        height: 64
        anchors.centerIn: parent

        borderColor: "green"
        backColor: "blue"
        borderWidth: 4
        radius: 20

        text:"my custom button"
        textColor: "black"
        fontSize: Math.round(blueRec.height/4)

    }

2.在MyButton.qml中的Item中命名id:button,并声明写过的属性

Item {
    id:button
    property string backColor
    property string borderColor
    property int borderWidth
    property int radius

    property string text
    property string textColor
    property int fontSize

3.在Item中写入Rec,描述属性参数,用main中MyButton中属性使用parent.,使用上述声明的属性,使用button(id).+name

Rectangle{
        id:blueRec
        color: button.backColor
        width: parent.width
        height:parent.height

        border.color:button.borderColor
        border.width: button.borderColor
        radius: button.radius
}

4.写入Text,同上,描述属性参数

Text {
        id:fooText
        anchors.centerIn: blueRec
        text: button.text
        color: button.textColor
        font.pixelSize: button.fontSize//
//        font.bold: true
    }

5.添加Mousearea的属性声明

//all the blueRecMouseArea propertys and signals

    //onEnter change's
    property string enteredColor
    property double enteredScale
    property string enteredBorderColor

    //onExit Changes
    property string exitedColor
    property double exitedScale
    property string exitedBorderColor

    property bool hoverEnabled: false

6.main函数中写明属性

        hoverEnabled: true
        enteredColor: "green"
        enteredScale: 1.5
        enteredBorderColor: "white"

        exitedColor: "blue"
        exitedScale: 1
        exitedBorderColor: "green"

7.MyButton函数中,在Item中加入MouseArea

MouseArea{
        id:blueRecMouseArea
        anchors.fill: blueRec
        hoverEnabled: button.hoverEnabled
        onEntered: {
            blueRec.scale = button.enteredScale
            blueRec.color =button.enteredColor
            blueRec.border.color = button.enteredBorderColor

        }
        onExited: {
            blueRec.scale = button.exitedScale
            blueRec.color =button.exitedColor
            blueRec.border.color = button.exitedBorderColor

        }


        onClicked: {
            console.log("hello form qml")
        }
    }

8.信号和槽的property,在MyButton函数中的Item属性中声明

signal buttonClicked

9.改变MouseA中的触发函数onClicked函数

onClicked: {
            buttonClicked()//console.log("hello form qml")
        }

10.在main函数写明属性值

 onButtonClicked: console.log("hello hah")

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值