QQmlPropertyMap 类

The QQmlPropertyMap class allows you to set key-value pairs that can be used in QML bindings. 更多...

头: #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
void freeze ()
void insert (const QString & key , const QVariant & value )
void insert (const QVariantHash & )
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 provides a convenient way to expose domain data to the UI layer. The following example shows how you might declare data in C++ and then access it in 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();
					

Then, in main.qml :

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

The binding is dynamic - whenever a key's value is updated, anything bound to that key will be updated as well.

To detect value changes made in the UI layer you can connect to the valueChanged () signal. However, note that valueChanged () 是 NOT emitted when changes are made by calling insert () 或 clear () - it is only emitted when a value is updated from QML.

注意: It is not possible to remove keys from the map; once a key has been added, you can only modify or clear its associated value.

注意: When deriving a class from QQmlPropertyMap, use the protected two-argument constructor which ensures that the class is correctly registered with the Qt 元对象系统 .

注意: The QMetaObject of a QQmlPropertyMap is dynamically generated and modified. Operations on that meta object are not thread safe, so applications need to take care to explicitly synchronize access to the meta object.

成员函数文档编制

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

Constructs a bindable map with parent object parent .

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

Constructs a bindable map with parent object parent . Use this constructor in classes derived from QQmlPropertyMap.

The type of derived is used to register the property map with the 元对象系统 , which is necessary to ensure that properties of the derived class are accessible. This type must be derived from QQmlPropertyMap.

[override virtual] QQmlPropertyMap:: ~QQmlPropertyMap ()

销毁可绑定映射。

void QQmlPropertyMap:: clear (const QString & key )

Clears the value (if any) associated with key .

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

Returns true if the map contains key .

另请参阅 size ().

int QQmlPropertyMap:: count () const

这是重载函数。

如同 size ().

[since 6.1] void QQmlPropertyMap:: freeze ()

Disallows any further properties to be added to this property map. Existing properties can be modified or cleared.

In turn, an internal cache is turned on for the existing properties, which may result in faster access from QML.

该函数在 Qt 6.1 引入。

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

Sets the value associated with key to value .

If the key doesn't exist, it is automatically created.

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

插入 values QQmlPropertyMap .

Keys that don't exist are automatically created.

This method is substantially faster than calling insert(key, value) many times in a row.

该函数在 Qt 6.1 引入。

bool QQmlPropertyMap:: isEmpty () const

Returns true if the map contains no keys; otherwise returns false.

另请参阅 size ().

[invokable] QStringList QQmlPropertyMap:: keys () const

Returns the list of keys.

Keys that have been cleared will still appear in this list, even though their associated values are invalid QVariants.

注意: 此函数可以被援引,通过元对象系统和从 QML。见 Q_INVOKABLE .

int QQmlPropertyMap:: size () const

Returns the number of keys in the map.

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

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

Returns the new value to be stored for the key key . This function is provided to intercept updates to a property from QML, where the value provided from QML is input .

Override this function to manipulate the property value as it is updated. Note that this function is only invoked when the value is updated from QML.

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

Returns the value associated with key .

If no value has been set for this key (or if the value has been cleared), an invalid QVariant 被返回。

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

This signal is emitted whenever one of the values in the map is changed. key is the key corresponding to the value that was changed.

注意: valueChanged() NOT emitted when changes are made by calling insert () 或 clear () - it is only emitted when a value is updated from QML.

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

Returns the value associated with the key key 作为可修改引用。

If the map contains no item with key key , the function inserts an invalid QVariant into the map with key key , and returns a reference to it.

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

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

这是重载函数。

如同 value ().