本页内容

QQmlPropertyMap Class

QQmlPropertyMap 类允许您设置可在 QML 绑定中使用的键值对。更多内容...

标题: #include <QQmlPropertyMap>
CMake: find_package(Qt6 REQUIRED COMPONENTS Qml)
target_link_libraries(mytarget PRIVATE Qt6::Qml)
qmake: QT += qml
继承自: QObject

公共函数

virtual ~QQmlPropertyMap() override
void clear(const QString &key)
bool contains(const QString &key) const
int count() const
(since 6.1) void freeze()
(since 6.1) void insert(const QVariantHash &values)
void insert(const QString &key, const QVariant &value)
bool isEmpty() const
QStringList keys() const
int size() const
QVariant value(const QString &key) const
QVariant &operator[](const QString &key)
QVariant operator[](const QString &key) const

信号

void valueChanged(const QString &key, const QVariant &value)

静态公共成员

QQmlPropertyMap *create(QObject *parent = nullptr)

受保护函数

QQmlPropertyMap(DerivedType *derived, QObject *parent)
virtual QVariant updateValue(const QString &key, const QVariant &input)

详细说明

QQmlPropertyMap 提供了一种将域数据暴露给 UI 层的便捷方式。以下示例展示了如何在 C++ 中声明数据,然后在 QML 中访问这些数据。

在 C++ 文件中:

// create our data
QQmlPropertyMap ownerData;
ownerData.insert("name", QVariant(QString("John Smith")));
ownerData.insert("phone", QVariant(QString("555-5555")));

// expose it to the UI layer
QQuickView view;
QQmlContext *ctxt = view.rootContext();
ctxt->setContextProperty("owner", &ownerData);

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

然后,在main.qml 中:

Text { text: owner.name + " " + owner.phone }

该绑定是动态的——每当某个键的值被更新时,与该键绑定的所有内容也会随之更新。

若要检测 UI 层中的值变化,可连接到 `valueChanged()` 信号。但请注意,当通过调用 `insert()` 或 `clear()` 进行更改时,不会发出 `valueChanged()` 信号——该信号仅在从 QML 更新值时才会发出。

注意:无法 从映射中删除键;一旦添加了键,您只能修改或清除其关联的值。

注意:当 从 QQmlPropertyMap 派生类时 ,请使用protected two-argument constructor ,以确保该类在 Qt元对象系统中正确注册。

注意: QQmlPropertyMap的 QMetaObject 是 动态生成和修改的 。对该元对象的操作并非线程安全,因此应用程序需要注意显式同步对元对象的访问。

成员函数文档

[protected] template <typename DerivedType> QQmlPropertyMap::QQmlPropertyMap(DerivedType *derived, QObject *parent)

构建一个以父对象parent 为基准的可绑定映射。请在从QQmlPropertyMap派生的类中使用此构造函数。

derived 的类型用于将属性映射注册到元对象系统中,这是确保能够访问派生类属性的必要条件。该类型必须继承自 QQmlPropertyMap。

在 C++ 文件中:

class MyQmlPropertyMap : public QQmlPropertyMap
{
    Q_OBJECT
    QML_NAMED_ELEMENT(MyQmlPropertyMap)
public:
    explicit MyQmlPropertyMap(QObject *parent = nullptr)
        : QQmlPropertyMap(this, parent)
    {
        insert("name", "John Smith");
        insert("phone", "555-5555");
        insert("email", "john.smith@example.com");
    }

public slots:
    void updateEmail(const QString &newEmail)
    {
        insert("email", newEmail);
    }
};
    QQuickView view;
    view.setSource(QUrl("qrc:/main.qml"));
    view.show();

然后,在main.qml 中:

MyQmlPropertyMap
{
    id : owner
    Component.onCompleted: { owner.updateEmail("new.email@example.com") }
}
Text { text : owner.name + " " + owner.phone + " " + owner.email }

[override virtual noexcept] QQmlPropertyMap::~QQmlPropertyMap()

销毁可绑定映射。

void QQmlPropertyMap::clear(const QString &key)

清除与key 关联的值(如有)。

bool QQmlPropertyMap::contains(const QString &key) const

如果该映射包含key ,则返回true。

另请参阅 size()。

int QQmlPropertyMap::count() const

与size() 相同。

[static] QQmlPropertyMap *QQmlPropertyMap::create(QObject *parent = nullptr)

创建一个可绑定的映射,其父对象为parent 。

[since 6.1] void QQmlPropertyMap::freeze()

禁止向此属性映射中添加任何其他属性。现有属性可以进行修改或清除。

相应地,系统会为现有属性启用内部缓存,这可能会加快 QML 的访问速度。

此功能自 Qt 6.1 起引入。

[since 6.1] void QQmlPropertyMap::insert(const QVariantHash &values)

将values 插入到QQmlPropertyMap 中。

不存在的键将自动创建。

此方法比连续多次调用 `insert(key, value) ` 要快得多。

该函数在 Qt 6.1 中引入。

void QQmlPropertyMap::insert(const QString &key, const QVariant &value)

将key 关联的值设置为value 。

如果该键不存在,则会自动创建。

bool QQmlPropertyMap::isEmpty() const

如果映射中不包含任何键,则返回 true;否则返回 false。

另请参阅 ` size()`。

[invokable] QStringList QQmlPropertyMap::keys() const

返回键的列表。

即使已清空的键,其关联的值已变为无效的 QVariant,这些键仍会出现在此列表中。

注意: 可以通过元对象系统和 QML 调用此 函数。参见Q_INVOKABLE 。

int QQmlPropertyMap::size() const

返回映射中的键的个数。

另请参阅 isEmpty() 和count()。

[virtual protected] QVariant QQmlPropertyMap::updateValue(const QString &key, const QVariant &input)

返回要存储在键key 下的新值。提供此函数是为了拦截来自QML的属性更新,其中QML提供的值为input 。

重写此函数可在属性更新时对其值进行操作。请注意,此函数仅在属性值由 QML 更新时才会被调用。

QVariant QQmlPropertyMap::value(const QString &key) const

返回与key 关联的值。

如果该键未设置任何值(或该值已被清空),则返回一个无效的QVariant 。

[signal] void QQmlPropertyMap::valueChanged(const QString &key, const QVariant &value)

每当地图中的某个值发生变化时,都会触发此信号。key 是与发生变化的value 对应的键。

注意: 当通过调用insert() 或clear() 进行更改时,不会触发valueChanged() 事件 ——该事件仅在从 QML 更新值时触发。

QVariant &QQmlPropertyMap::operator[](const QString &key)

返回与键key 关联的值,该值以可修改引用形式返回。

如果映射中不存在键为key 的项,则该函数会向映射中插入一个键为key 的无效QVariant ,并返回对其的引用。

另请参阅 insert() 和value()。

QVariant QQmlPropertyMap::operator[](const QString &key) const

与value()相同。

这是一个重载函数。

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