QML入门开发3-QML基本语法和如何查看帮助文档

1新建一个QML工程

2添加如下代码

import QtQuick
import QtQuick.Controls

Window {
    width: 640
    height: 480
    visible: true
    title: qsTr("Hello World")
    //矩形
    Rectangle{
        id:redRectanggle
        width: roundRectangg.width
        height: roundRectangg.height
        anchors.top: parent.top
        anchors.topMargin:  10
        anchors.horizontalCenter: parent.horizontalCenter
        color: "red"
    }
    //圆形
    Rectangle{
        id:roundRectangg
        width: 100
        height: 100
        anchors.centerIn:parent
        anchors.margins: 10
        anchors.horizontalCenter: parent.horizontalCenter
        color: "green"
        radius: 0.5 * width
    }

    /*
    //椭圆
    Rectangle{
        id:ellipse
        width: 150
        height: 100
        anchors.centerIn:parent
        anchors.margins: 10
        anchors.horizontalCenter: parent.horizontalCenter
        color: "green"
        radius: 0.5 * width
    }
    */

    //add 按钮
    Button{
        id:addButton
        text:"add"
        font.pixelSize: 20
        width: 100
        height: 40

        onClicked: {
            console.log("add "+roundRectangg.width )
            roundRectangg.width +=10

        }
        anchors.bottom: parent.bottom
        anchors.bottomMargin: 10
        anchors.right: parent.right
        anchors.rightMargin: 100
    }
    //sub 按钮
    Button{
        id:subButton
        text:"sub"
        font.pixelSize: 20
        width: 100
        height: 40

        onClicked: {
            console.log("add "+roundRectangg.width )
            roundRectangg.width -=10
        }

        anchors.bottom: parent.bottom
        anchors.bottomMargin: 10

        anchors.left: parent.left
        anchors.leftMargin:  100
    }
}

3相关的QML代码解释

3.1包含 import

import QtQuick
import QtQuick.Controls

The Qt Quick module provides graphical primitive types. These types are only available in a QML document if that document imports the QtQuick namespace.

类似于命名空间,QtQuick命名空间里面的关键次 如 Rectangle、windows可以访问。QtQuick.Controls命名空间里可以访问Button

3.2对象 object

Rectangle、windows、的基类均为object

Rectangle-->item-->QtObject

3.3属性 property

height

weight

text

用于描述Object的样式

3.4布局 anchors

  anchors.top: parent.top
  anchors.topMargin:  10
  anchors.horizontalCenter: parent.horizontalCenter
 

3.5注释

//

/**/

同c/c++代码的注释

3.6属性绑定

        width: roundRectangg.width
        height: roundRectangg.height

一个object绑定另一个object的属性

3.7表达式

 radius: 0.5 * width

 roundRectangg.width +=10

类似于C原因的语句

3.8调试

  console.log("add "+roundRectangg.width )
 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值