当前位置: 首页 > news >正文

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 )
 

相关文章:

  • 机器人原点丢失后找回原点的解决方案与步骤
  • 操作系统 2.12-死锁处理
  • 用免费的github的key调用gpt实现一个简单的rag自动打分评测系统,不用任何框架
  • [计算机三级网络技术]第七章:路由器配置及使用
  • 2025年移动端开发性能优化实践与趋势分析
  • 一文详解redis
  • 【从零实现Json-Rpc框架】- 第三方库介绍 - jsoncpp篇
  • Apache Dubbo 与 ZooKeeper 集成:服务注册与发现的全解析
  • 复杂任务需要多agent协同处理,对其进行逻辑编排和参数调用
  • JVM 02
  • HarmonyOS Next~鸿蒙应用框架开发实战:Ability Kit与Accessibility Kit深度解析
  • 【leetcode hot 100 20】有效的括号
  • uniapp动态循环表单校验失败:初始值校验
  • 【学习资源】多元时序分析问题和时序图模型
  • 【机器学习】建模流程
  • uniapp笔记-swiper组件实现轮播图
  • Unicode代码点与编码
  • 好消息!软航文档控件(NTKO WebOffice)在Chrome 133版本上提示扩展已停用的解决方案
  • 【虚拟环境配置】地信从0开始的环境配置小白教程
  • python康复日记-request库的使用,爬虫自动化测试
  • 财政部部长蓝佛安:中国将采取更加积极有为的宏观政策
  • 财政部、证监会:加强对会计师事务所从事证券服务业务的全流程监管
  • 今年五一,贵州一脸“爆相”
  • 中国海外宏洋集团:一季度经营溢利同比降48.6%,密切关注行业收并购机会等
  • 著名哲学家、中山大学哲学系原系主任李锦全逝世
  • 稀土管制难倒特斯拉人形机器人“擎天柱”,马斯克:“正与中国协商”