QQmlListProperty Class

template <typename T> class QQmlListProperty

The QQmlListProperty class allows applications to expose list-like properties of QObject -derived classes to QML. 更多...

头: #include <QQmlListProperty>
CMake: find_package(Qt6 COMPONENTS Qml REQUIRED)
target_link_libraries(mytarget PRIVATE Qt6::Qml)
qmake: QT += qml
Since: Qt 5.0

公共类型

  AppendFunction
  AtFunction
  ClearFunction
  CountFunction
  RemoveLastFunction
  ReplaceFunction

公共函数

  QQmlListProperty (QObject * object , void * data , QQmlListProperty::CountFunction count , QQmlListProperty::AtFunction at )
  QQmlListProperty (QObject * object , void * data , QQmlListProperty::AppendFunction append , QQmlListProperty::CountFunction count , QQmlListProperty::AtFunction at , QQmlListProperty::ClearFunction clear , QQmlListProperty::ReplaceFunction replace , QQmlListProperty::RemoveLastFunction removeLast )
  QQmlListProperty (QObject * object , void * data , QQmlListProperty::AppendFunction append , QQmlListProperty::CountFunction count , QQmlListProperty::AtFunction at , QQmlListProperty::ClearFunction clear )
  QQmlListProperty (QObject * object , QList<T *> * list )
bool operator== (const QQmlListProperty<T> & other ) const

  QML_LIST_PROPERTY_ASSIGN_BEHAVIOR_APPEND
  QML_LIST_PROPERTY_ASSIGN_BEHAVIOR_REPLACE
  QML_LIST_PROPERTY_ASSIGN_BEHAVIOR_REPLACE_IF_NOT_DEFAULT

详细描述

QML has many list properties, where more than one object value can be assigned. The use of a list property from QML looks like this:

FruitBasket {
    fruit: [
        Apple {},
        Orange{},
        Banana{}
    ]
}
					

The QQmlListProperty encapsulates a group of function pointers that represent the set of actions QML can perform on the list - adding items, retrieving items and clearing the list. In the future, additional operations may be supported. All list properties must implement the append operation, but the rest are optional.

To provide a list property, a C++ class must implement the operation callbacks, and then return an appropriate QQmlListProperty value from the property getter. List properties should have no setter. In the example above, the Q_PROPERTY () declarative will look like this:

Q_PROPERTY(QQmlListProperty<Fruit> fruit READ fruit)
					

QML list properties are type-safe - in this case Fruit QObject type that Apple , Orange and Banana all derive from.

另请参阅 扩展 QML - 对象和列表特性类型范例 .

成员类型文档编制

[alias] QQmlListProperty:: AppendFunction

同义词 void (*)(QQmlListProperty<T> *property, T *value) .

Append the value to the list property .

[alias] QQmlListProperty:: AtFunction

同义词 T *(*)(QQmlListProperty<T> *property, qsizetype index) .

Return the element at position index in the list property .

[alias] QQmlListProperty:: ClearFunction

同义词 void (*)(QQmlListProperty<T> *property) .

Clear the list property .

[alias] QQmlListProperty:: CountFunction

同义词 qsizetype (*)(QQmlListProperty<T> *property) .

Return the number of elements in the list property .

[alias] QQmlListProperty:: RemoveLastFunction

同义词 void (*)(QQmlListProperty<T> *property) .

Remove the last element from the list property .

[alias] QQmlListProperty:: ReplaceFunction

同义词 void (*)(QQmlListProperty<T> *property, qsizetype index, T *value) .

Replace the element at position index in the list property with value .

成员函数文档编制

QQmlListProperty:: QQmlListProperty ( QObject * object , void * data , QQmlListProperty::CountFunction count , QQmlListProperty::AtFunction at )

Construct a readonly QQmlListProperty from a set of operation functions count and at . An opaque data handle may be passed which can be accessed from within the operation functions. The list property remains valid while object exists.

QQmlListProperty:: QQmlListProperty ( QObject * object , void * data , QQmlListProperty::AppendFunction append , QQmlListProperty::CountFunction count , QQmlListProperty::AtFunction at , QQmlListProperty::ClearFunction clear , QQmlListProperty::ReplaceFunction replace , QQmlListProperty::RemoveLastFunction removeLast )

Construct a QQmlListProperty from a set of operation functions append , count , at , clear , replace , and removeLast. An opaque data handle may be passed which can be accessed from within the operation functions. The list property remains valid while object exists.

Null pointers can be passed for any function, causing the respective function to be synthesized using the others, if possible. QQmlListProperty can synthesize

  • clear 使用 count and removeLast
  • replace 使用 count , at , clear ,和 append
  • replace 使用 count , at , removeLast ,和 append
  • removeLast 使用 count , at , clear ,和 append

if those are given. This is slow, but if your list does not natively provide faster options for these primitives, you may want to use the synthesized ones.

Furthermore, if either of count , at , append ,和 clear are neither given explicitly nor synthesized, the list will be neither designable nor alterable by the debugger. It is recommended to provide enough valid pointers to avoid this situation.

QQmlListProperty:: QQmlListProperty ( QObject * object , void * data , QQmlListProperty::AppendFunction append , QQmlListProperty::CountFunction count , QQmlListProperty::AtFunction at , QQmlListProperty::ClearFunction clear )

Construct a QQmlListProperty from a set of operation functions append , count , at ,和 clear . An opaque data handle may be passed which can be accessed from within the operation functions. The list property remains valid while object exists.

Null pointers can be passed for any function. If any null pointers are passed in, the list will be neither designable nor alterable by the debugger. It is recommended to provide valid pointers for all functions.

注意: The resulting QQmlListProperty will synthesize the removeLast() and replace() methods using count , at , clear ,和 append if all of those are given. This is slow. If you intend to manipulate the list beyond clearing it, you should explicitly provide these methods.

[since 5.15] QQmlListProperty:: QQmlListProperty ( QObject * object , QList < T *> * list )

Convenience constructor for making a QQmlListProperty value from an existing QList list list reference must remain valid for as long as object exists. object must be provided.

该函数在 Qt 5.15 引入。

bool QQmlListProperty:: operator== (const QQmlListProperty < T > & other ) const

返回 true,若此 QQmlListProperty 等于 other ,否则 false。

宏文档编制

QML_LIST_PROPERTY_ASSIGN_BEHAVIOR_APPEND

This macro defines the behavior of the list properties of this class to Append. When assigning the property in a derived type, the values are appended to those of the base class. This is the default behavior.

class FruitBasket : QObject {
    Q_OBJECT
    QML_LIST_PROPERTY_ASSIGN_BEHAVIOR_APPEND
    Q_PROPERTY(QQmlListProperty<Fruit> fruit READ fruit)
    public:
    // ...
    QQmlListProperty<Fruit> fruit();
    // ...
};
					

另请参阅 QML_LIST_PROPERTY_ASSIGN_BEHAVIOR_REPLACE_IF_NOT_DEFAULT and QML_LIST_PROPERTY_ASSIGN_BEHAVIOR_REPLACE .

QML_LIST_PROPERTY_ASSIGN_BEHAVIOR_REPLACE

This macro defines the behavior of the list properties of this class to Replace. When assigning the property in a derived type, the values replace those of the base class.

class FruitBasket : QObject {
    Q_OBJECT
    QML_LIST_PROPERTY_ASSIGN_BEHAVIOR_REPLACE
    Q_PROPERTY(QQmlListProperty<Fruit> fruit READ fruit)
    public:
    // ...
    QQmlListProperty<Fruit> fruit();
    // ...
};
					

另请参阅 QML_LIST_PROPERTY_ASSIGN_BEHAVIOR_APPEND and QML_LIST_PROPERTY_ASSIGN_BEHAVIOR_REPLACE_IF_NOT_DEFAULT .

QML_LIST_PROPERTY_ASSIGN_BEHAVIOR_REPLACE_IF_NOT_DEFAULT

This macro defines the behavior of the list properties of this class to ReplaceIfNotDefault. When assigning the property in a derived type, the values replace those of the base class unless it's the default property. In the case of the default property, values are appended to those of the base class.

class FruitBasket : QObject {
    Q_OBJECT
    QML_LIST_PROPERTY_ASSIGN_BEHAVIOR_REPLACE_IF_NOT_DEFAULT
    Q_PROPERTY(QQmlListProperty<Fruit> fruit READ fruit)
    public:
    // ...
    QQmlListProperty<Fruit> fruit();
    // ...
};
					

另请参阅 QML_LIST_PROPERTY_ASSIGN_BEHAVIOR_APPEND and QML_LIST_PROPERTY_ASSIGN_BEHAVIOR_REPLACE .