QML Button组件 及 定位器

新建Button.qml

import QtQuick 2.0

Rectangle{

    id:root
    //定义属性别名 text  值为label的text
    property alias text: label.text
    //定义信号名字  利用on实现调用函数
    //signal clicked
    signal myClicked

    width: 116; height: 26
    color: "lightsteelblue"
    border.color: "slategrey"
    radius:8

    Text {
        id: label
        anchors.centerIn: parent
        text: "Start"
    }
    MouseArea {
        anchors.fill: parent
        onClicked: {
            //root.clicked()
            root.myClicek()
        }
    }
}

主界面中调用:

import QtQuick 2.0
import QtQuick.Window 2.2
Window{
    width: 300
    height: 300
    visible: true
    Button{
        id:button
        x:12
        y:12
        //这里的text调用的是Button中定义的属性别名property alias text
        //如未定义 报错Cannot assign to non-existent property "text"
        text: "start"
        color: "blue"
        //onClicked: 
        //利用on调用Button里定义的信号
        onMyClicked:{
            status.text = "gagagga...."
            button.color ="cyan"
        }
    }
 }
    Text {
        id: status
        x:12
        y:76
        width: 116
        height: 26
        text: qsTr("waiting...")
        anchors.centerIn: parent
    }
}

定位器(Row{} Column{} Grid{} FLow{})
定义三个组件
BlueRectangle

import QtQuick 2.0

Rectangle { 
    width: 48
    height: 48
    color: "blue"
    //Qt.lighter(color)  颜色变亮
    border.color: Qt.lighter(color)
}

GreenRectangle

import QtQuick 2.0

Rectangle {
    width: 48
    height: 48
    color: "green"
    border.color: Qt.lighter(color)
}

RedRectangle

import QtQuick 2.0

Rectangle {
    width: 48
    height: 48
    color: "red"
    border.width: 5
    border.color: Qt.lighter(color)
}

列定位:

    Column{
        id:col
        anchors.centerIn: parent
        spacing: 8
        RedRectangle{

        }
        BlueRectangle{
            width: 94
        }
        GreenRectangle{

        }
    }

行定位:

Row{
        id:row
        anchors.centerIn: parent
        spacing: 8
        RedRectangle{

        }
        BlueRectangle{
            width: 94
        }
        GreenRectangle{

        }
    }

这里写图片描述
网格Grid定位:

Grid{
        id:grid
        anchors.centerIn: parent
        //设置网格的行数
        //rows:2
        //设置网格的列数
        //columns: 2
        //不设置时网格默认列数为4
        spacing: 8
        RedRectangle { }
        RedRectangle { }
        RedRectangle { }
        RedRectangle { }
        RedRectangle { }
    }

Flow 结合LayoutDirection控制显示方式(流布局 宽度不够自动换行)

Flow {
        anchors.fill: parent
        //marigins 针对的是父元素的margin
        anchors.margins: 20
        spacing: 20
        RedRectangle { }
        BlueRectangle { }
        GreenRectangle { }
    }

Repeat 重复

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值