QtObject QML Type

一种基本的 QML 类型。更多

Import Statement: import QtQml
In C++: QObject

属性

详细说明

QtObject 类型是一种非可视元素,只包含objectName 属性。

如果您需要一种极其轻量级的类型来封装一组自定义属性,那么创建 QtObject 将会非常有用:

import QtQuick

Item {
    QtObject {
        id: attributes
        property string name
        property int size
        property variant attributes
    }

    Text { text: attributes.name }
}

它也可用于 C++ 集成,因为它只是一个普通的QObject 。更多详情,请参阅QObject 文档。

属性文档

objectName : string

该属性包含此特定对象实例的QObject::objectName

这允许 C++ 应用程序使用QObject::findChild() 方法定位 QML 组件中的项目。例如,下面的 C++ 应用程序定位子Rectangle 项,并动态更改其color 值:

// MyRect.qml

import QtQuick 2.0

Item {
    width: 200; height: 200

    Rectangle {
        anchors.fill: parent
        color: "red"
        objectName: "myRect"
    }
}
// main.cpp

QQuickView view;
view.setSource(QUrl::fromLocalFile("MyRect.qml"));
view.show();

QQuickItem *item = view.rootObject()->findChild<QQuickItem*>("myRect");
if (item)
    item->setProperty("color", QColor(Qt::yellow));

© 2025 The Qt Company Ltd. Documentation contributions included herein are the copyrights of their respective owners. The documentation provided herein is licensed under the terms of the GNU Free Documentation License version 1.3 as published by the Free Software Foundation. Qt and respective logos are trademarks of The Qt Company Ltd. in Finland and/or other countries worldwide. All other trademarks are property of their respective owners.