【Qt之Quick模块】5. QML基本类型及示例用法

QML格式

QML基本类型

在 QML 中,有以下基本类型:

  1. int:整数类型。
cpp 复制代码
Rectangle {
    function myFunction() {
        // 输出 debug 信息
        console.log("1+1 =" + (1+1));
    }

    Component.onCompleted: {
        myFunction();
    }
}

结果:

  1. real:浮点类型。

  2. double:双精度浮点类型。

  3. string:字符串类型。

cpp 复制代码
Rectangle {
    function myFunction() {
        // 输出 debug 信息
        console.log("helloworld");
    }

    Component.onCompleted: {
        myFunction();
    }
}

结果:

  1. bool:布尔类型。

  2. color:颜色类型,用于表示颜色的RGBA值。

  3. var:通用类型,可以表示任意类型的数据。

cpp 复制代码
    Item {
        property var myVar: "Hello World"

        Component.onCompleted: {
            console.log(myVar) // 输出 "Hello World" 到控制台
        }
    }

结果:

  1. date:日期类型。
cpp 复制代码
Rectangle {
    Item {
        property var currentDate: new Date()

        Component.onCompleted: {
            console.log(currentDate.toString()) // 输出当前日期和时间到控制台
        }
    }
}

结果:

  1. point:点类型,用于表示二维空间中的点。
cpp 复制代码
Item {
    width: 200
    height: 200

    property var point: Qt.point(50, 100)

    Component.onCompleted: {
        console.log(point.x, point.y) // 输出点对象的坐标值到控制台
    }
}

结果:

  1. size:尺寸类型,用于表示宽度和高度。
cpp 复制代码
Item {
    width: 200
    height: 200

    property size var_size: Qt.size(0, 2)

    Component.onCompleted: {
        console.log(var_size) // 输出点对象的坐标值到控制台
    }
}

结果:

  1. rect:矩形类型,用于表示矩形区域的左上角坐标和宽高。
cpp 复制代码
Item {
    width: 200
    height: 200

    property rect var_rect: Qt.rect(0, 0, 1, 2)

    Component.onCompleted: {
        console.log(var_rect) // 输出点对象的坐标值到控制台
    }
}

结果:

相关推荐
清汤饺子几秒前
Everything Claude Code:让我把 AI 编程效率再翻一倍的东西
前端·javascript·后端
CoderCodingNo几秒前
【GESP】C++八级考试大纲知识点梳理 (7) 算法的时间和空间效率分析
开发语言·c++·算法
程序员zgh几秒前
C++ 环形队列 从原理到实例演示
c语言·开发语言·数据结构·c++·学习
code_计梦星河1 分钟前
Qt 开发第十三天:打卡模块跨端数据传输及连续天数计算开发
qt
csbysj20201 分钟前
Bootstrap 下拉菜单:全面解析与应用指南
开发语言
誰能久伴不乏5 分钟前
从数字世界到物理引擎:用 PWM 撕开 0 和 1 的结界
linux·arm开发·c++·qt
西洼工作室9 分钟前
React TabBar切换与高亮实现
前端·javascript·react.js
belldeep12 分钟前
前端:Bootstrap 3.0 , 4.0 , 5.0 有什么差别?
前端·bootstrap·html
sycmancia17 分钟前
QT——第一个GUI应用程序
qt
wuhen_n17 分钟前
Tool Schema 设计模式详解
前端·javascript·ai编程