QDeclarativePropertyMap Class Reference

[ QtDeclarative module]

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

继承 QObject .

方法

Special Methods

Qt Signals


详细描述

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

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

In the C++ file:

 // create our data
 QDeclarativePropertyMap ownerData;
 ownerData.insert("name", QVariant(QString("John Smith")));
 ownerData.insert("phone", QVariant(QString("555-5555")));
 // expose it to the UI layer
 QDeclarativeView view;
 QDeclarativeContext *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.


方法文档编制

QDeclarativePropertyMap.__init__ ( self , QObject   parent  = None)

parent argument, if not None, causes self to be owned by Qt instead of PyQt.

Constructs a bindable map with parent object parent .

QDeclarativePropertyMap.clear ( self , QString  key )

Clears the value (if any) associated with key .

bool QDeclarativePropertyMap.contains ( self , QString  key )

Returns true if the map contains key .

另请参阅 size ().

int QDeclarativePropertyMap.count ( self )

这是重载函数。

如同 size ().

QDeclarativePropertyMap.insert ( self , QString  key , QVariant  value )

Sets the value associated with key to value .

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

bool QDeclarativePropertyMap.isEmpty ( self )

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

另请参阅 size ().

QStringList QDeclarativePropertyMap.keys ( self )

Returns the list of keys.

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

int QDeclarativePropertyMap.size ( self )

Returns the number of keys in the map.

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

QVariant QDeclarativePropertyMap.value ( self , QString  key )

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 is returned.

QVariant QDeclarativePropertyMap.__getitem__ ( self , QString  key )

QDeclarativePropertyMap.__len__ ( self )


Qt Signal Documentation

void valueChanged (const QString&,const QVariant&)

This is the default overload of this signal.

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

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