QML组件的使用

声明属性

/*
    *声明一个信号函数 变量名是cellColor 值是rectangle.color的值
    *alias就是自动解析类型
    */
    property alias cellColor: rectangle.color

声明信号函数

 /*
     * 声明一个信号函数
    */
    signal clicked(color cellcolor)

响应鼠标事件

MouseArea{
        anchors.fill: parent
        onClicked: container.clicked(container.cellcolor)
    }

响应槽函数

 /*
         * 处理cell的信号事件,如果cell发送了信号就设置Text的值
        */
        Cell { cellColor: "red"; onClicked: helloText.color = cellColor }
        Cell { cellColor: "green"; onClicked: helloText.color = cellColor }
        Cell { cellColor: "blue"; onClicked: helloText.color = cellColor }
        Cell { cellColor: "yellow"; onClicked: helloText.color = cellColor }
        Cell { cellColor: "steelblue"; onClicked: helloText.color = cellColor }
        Cell { cellColor: "black"; onClicked: helloText.color = cellColor }

声明组件

import QtQuick 2.0
import QtQuick.Controls 2.2
/*
*组件名字的首字母都必须是大写的
*/
Item {
    id:container
    /*
    *声明一个信号函数 变量名是cellColor 值是rectangle.color的值
    *alias就是自动解析类型
    */
    property alias cellColor: rectangle.color
    /*
     * 声明一个信号函数
    */
    signal clicked(color cellcolor)
    width: 40
    height: 25
    Rectangle{
        id:rectangle
        border.color: "white"
        anchors.fill: parent
        height:25
    }
    MouseArea{
        anchors.fill: parent
        onClicked: container.clicked(container.cellcolor)
    }
}

使用组件

import QtQuick 2.0
import QtQuick.Controls 2.2
Rectangle {
    id:page
    width: 320
    height: 480
    color: "lightgray"
    Text{
        id:helloText
        text:"hello world"
        anchors.topMargin: 30
        anchors.top: page.top
        anchors.horizontalCenter: page.horizontalCenter
    }
    Grid {
        id: colorPicker
        x: 4
        anchors.bottom: page.bottom
        anchors.bottomMargin: 4
        rows: 2
        columns: 3
        spacing: 3
        /*
         * 处理cell的信号事件,如果cell发送了信号就设置Text的值
        */
        Cell { cellColor: "red"; onClicked: helloText.color = cellColor }
        Cell { cellColor: "green"; onClicked: helloText.color = cellColor }
        Cell { cellColor: "blue"; onClicked: helloText.color = cellColor }
        Cell { cellColor: "yellow"; onClicked: helloText.color = cellColor }
        Cell { cellColor: "steelblue"; onClicked: helloText.color = cellColor }
        Cell { cellColor: "black"; onClicked: helloText.color = cellColor }

    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值