QQmlPropertyMap Class

QQmlPropertyMap 类允许您设置可用于 QML 绑定的键值对。更多

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

公共函数

QQmlPropertyMap(QObject *parent = nullptr)
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(DerivedType *derived, QObject *parent)
virtual QVariant updateValue(const QString &key, const QVariant &input)

详细说明

QQmlPropertyMap 提供了一种方便的方式,将域数据暴露给用户界面层。下面的例子展示了如何在 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 ,它能确保该类在 QtMeta-Object System 中正确注册。

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

成员函数文档

[explicit] QQmlPropertyMap::QQmlPropertyMap(QObject *parent = nullptr)

通过父对象parent 构建一个可绑定的映射。

[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() 相同。

[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

返回键值列表。

已被清除的键仍会出现在此列表中,即使它们的相关值是无效的 QVariants。

注: 可通过元对象系统和 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)

每当 map 中的一个值发生变化时,就会发出这个信号。key 是与value 对应的键。

注意: valueChanged()不会在调用insert() 或clear() 进行更改时发出,它只会在从 QML 更新值时发出。

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

以可修改引用的形式返回与 keykey 相关联的值。

如果映射表中不包含键值为key 的项目,则函数会在映射表中插入一个无效的QVariant ,键值为key ,并返回对它的引用。

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

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

这是一个重载函数。

value() 相同。

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