本页内容

QtObject QML Type

一种基本的 QML 类型。更多...

Import Statement: import QtQml
In C++: QObject
Inherited By:
135 types

AbstractButton, Action, ActionGroup, AllOfFilter, AnimatedImage, AnimatedSprite, AnyOfFilter, BorderImage, BusyIndicator, Button, ButtonGroup, Calendar, Canvas, CheckBox, CheckDelegate, ColorGroup, ColorOpacityAnimation, Column, ColumnLayout, ComboBox, Container, Control, DayOfWeekRow, DelayButton, Dial, Dialog, DialogButtonBox, DoubleSpinBox, Drawer, DropArea, EllipseShape, FilterBase, FlexboxLayout, Flickable, Flipable, Flow, FocusScope, Frame, FunctionFilter, FunctionSorter, Grid, GridLayout, GridView, GroupBox, HorizontalHeaderView, HorizontalHeaderViewDelegate, Image, ImageParticle, Item, ItemDelegate, ItemGrabResult, ItemParticle, Label, LayoutItemProxy, ListView, Loader, MaskShape, Menu, MenuBar, MenuBarItem, MenuItem, MenuSeparator, MonthGrid, MouseArea, MultiEffect, MultiPointTouchArea, Overlay, Page, PageIndicator, Palette, Pane, ParticlePainter, PathView, PinchArea, Popup, ProgressBar, RadioButton, RadioDelegate, RangeFilter, RangeSlider, Rectangle, RectangularShadow, RegExpFilter, Repeater, RoleFilter, RoleSorter, RoundButton, Row, RowLayout, ScrollBar, ScrollIndicator, ScrollView, SearchField, SelectionRectangle, Settings, ShaderEffect, ShaderEffectSource, Shape, Slider, Sorter, SpinBox, SplitHandle, SplitView, SpriteSequence, StackLayout, StackView, StandardPaths, StringSorter, SwipeDelegate, SwipeView, Switch, SwitchDelegate, SystemInformation, TabBar, TabButton, TableView, TableViewDelegate, Text, TextArea, TextEdit, TextField, TextInput, ToolBar, ToolButton, ToolSeparator, ToolTip, TreeView, TreeViewDelegate, Tumbler, ValueFilter, VectorImage, VerticalHeaderView, VerticalHeaderViewDelegate, WeekNumberColumn, and WindowContainer

属性

详细说明

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

如果您需要一种极其轻量级的类型来封装一组自定义属性,创建一个 QtObject 可能会很有用:

import QtQuick

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

    Text { text: attributes.name }
}

此外,由于它只是一个普通的QObject ,因此对于 C++ 集成也很有用。更多详情请参阅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));

© 2026 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.