QML Item 元素

Item 是 QML 中最基础的视觉元素,作为所有可视组件的基类,它提供了基本的属性和功能,但不具有可视化表现(没有颜色、边框等)。

核心属性

属性 类型 默认值 描述
x real 0 X 坐标位置
y real 0 Y 坐标位置
width real 0 宽度
height real 0 高度
z real 0 Z 轴顺序(堆叠顺序)
opacity real 1.0 透明度(0.0-1.0)
visible bool true 是否可见
enabled bool true 是否接受用户输入
clip bool false 是否裁剪子项超出部分

布局相关属性

属性 类型 描述
anchors AnchorLine 锚定系统
baselineOffset real 基线偏移
transform list<Transform> 变换列表
transformOrigin enumeration 变换原点

常用信号

信号 描述
onXChanged X 坐标改变时触发
onYChanged Y 坐标改变时触发
onWidthChanged 宽度改变时触发
onHeightChanged 高度改变时触发
onChildrenChanged 子项变化时触发

基本用法

qml

复制代码
import QtQuick 2.15

Item {
    id: rootItem
    width: 400
    height: 300
    
    // 作为容器使用
    Item {
        x: 50
        y: 50
        width: 200
        height: 200
        
        Rectangle {
            width: 100
            height: 100
            color: "red"
        }
    }
    
    // 不可见的交互区域
    Item {
        width: 50
        height: 50
        x: 300
        y: 200
        
        MouseArea {
            anchors.fill: parent
            onClicked: console.log("点击了不可见区域")
        }
    }
}

作为组件基类

qml

复制代码
// MyComponent.qml
Item {
    property alias color: rect.color
    property string text
    
    width: 150
    height: 50
    
    Rectangle {
        id: rect
        anchors.fill: parent
        color: "lightblue"
    }
    
    Text {
        text: parent.text
        anchors.centerIn: parent
    }
}

// 使用自定义组件
MyComponent {
    text: "自定义项"
    color: "salmon"
}
相关推荐
大橘20 小时前
【qml-5】qml与c++交互(类型单例)
qt·qml
大橘1 天前
【qml-4】qml与c++交互(类型多例)
qt·qml
钱彬 (Qian Bin)11 天前
AI质检数据准备利器:基于Qt/QML 5.14的图像批量裁剪工具开发实战
qt·自定义·图像·qml·qt quick·裁剪工具
钱彬 (Qian Bin)21 天前
《使用Qt Quick从零构建AI螺丝瑕疵检测系统》——8. AI赋能(下):在Qt中部署YOLOv8模型
人工智能·qt·yolo·qml·qt quick·工业质检·螺丝瑕疵检测
Little-Hu23 天前
QML 3D曲面图(Surface3D)技术
3d·qml
Little-Hu25 天前
QML视图组件:ListView、GridView、TableView、PathView
数据库·microsoft·qml
钱彬 (Qian Bin)1 个月前
《使用Qt Quick从零构建AI螺丝瑕疵检测系统》——6. 传统算法实战:用OpenCV测量螺丝尺寸
教程·cmake·qml·qt quick·工业软件·工业瑕疵检测·qt6.9.1
钱彬 (Qian Bin)1 个月前
《使用Qt Quick从零构建AI螺丝瑕疵检测系统》——4. 前后端联动:打通QML与C++的任督二脉
c++·qt·教程·qml·qt quick·qt 6.9.1·工业瑕疵检测
钱彬 (Qian Bin)1 个月前
《使用Qt Quick从零构建AI螺丝瑕疵检测系统》——0. 博客系列大纲
人工智能·qt·qml·瑕疵检测·qt quick·yolo8·工业质检
丁劲犇1 个月前
Qt Graphs 模块拟取代 charts 和 data visualization还有很长的路要走
c++·qt·qml·visualization·charts·graphs