C
Qul::ListProperty Class
template <typename T> class Qul::ListPropertyUse the ListProperty class to hold a list of a given type. More...
| Header: | #include <qul/listproperty.h> |
| Since: | Qt Quick Ultralite 2.9 |
Public Functions
| ListProperty() | |
| void | append(T t) |
| typename IList<T>::const_reference | at(int index) const |
| bool | canGrow() const |
| void | clear() |
| int | count() const |
| T | get(int index) const |
| Qul::IList<T> & | getList() const |
| bool | hasList() const |
| void | load(const Qul::ListProperty<U> &otherList) |
| bool | removeAt(int index) |
| void | removeLast() |
| void | replace(int index, T newValue) |
| void | resize(int newSize) |
| void | setList(Qul::IList<T> *l, bool silent = false) |
| void | syncList() |
| Qul::ListProperty<T> & | operator=(const L &otherList) |
| typename IList<T>::reference | operator[](int index) |
| typename IList<T>::const_reference | operator[](int index) const |
Detailed Description
Use this class as a public member of your C++ objects, which are exposed as list of properties for that object in QML. The template parameter of the class defines the C++ type and is mapped to a corresponding QML type.
struct MyObjectWithList : public Qul::Object
{
Qul::ListProperty<int> list1;
Qul::ListProperty<MyData *> list2;
MyObjectWithList()
{
static Qul::StaticListFixed<int, 100> buffer;
buffer.replace(0, 1);
// ... further initialization
list1.setList(&buffer);
}
};You can use the C++ list properties in QML bindings.
As you can see in the following example, it's possible to assign JavaScript arrays to lists
MyObjectWithList {
// list1 is declared and initiated in c++
property int value: list1[0]
// list2 is declared in c++ and initiated here
list2: [
MyData { val: 100 },
MyData { val: 200 }
]
}It is also possible to declare the list in Grouped properties
struct MyObject : public Qul::Object
{
struct Grouped
{
Qul::Property<int> val1;
Qul::Property<int> val2;
Qul::ListProperty<int> list1;
};
Grouped group;
};List storage
In QUL it is possible to choose between two storage types, depending on whether the list has to grow or not.
You can set the storage explicitly in cpp on a Qul::ListProperty by using the setList function as shown here
DynamicList
A DynamicList is the default storage for QML lists without special keywords. Qt Quick Ultralite stores the items in the heap using a linked list. Use DynamicList only when you know that the list should grow, ohterwise use static_list instead.
Qul::ListProperty<int> list;
list.setList(new Qul::DynamicList<int>());StaticList
To avoid heap allocation, you can use StaticList storage instead. A StaticList only needs the array pointer and its size to statically allocate memory, reducing memory overhead at runtime.
//... declaration
Qul::ListProperty<int> list;
int data[10];
Qul::StaticList<int> storage;
//... initialization
storage.size = 10;
storage.data = data;
list.setList(storage);StaticListFixed
As it is a recurring pattern to declare an array along with a StaticList, and initialize both. You can use StaticListFixed (inherits StaticList) to simplify that, so you can use it as a storage for a ListProperty.
Unlike StaticList which depends on an external pointer not managed by it, a StaticListFixed manages the array internally and cleans the array when it is destroyed.
// ... declaration
Qul::ListProperty<int> list;
// T t[N=10] will be added as a member of the StaticListFixed
Qul::StaticListFixed<int, 10> staticList;
// ... initialization and usage
list.setList(&staticList);Supported operations
ListProperty supports the following operations. Depending on the storage used, some operations may not be possible, so it is necessary to set the correct storage before using these restricted functions.
append(): Adds a new item to the end of the list, supported onDynamicListonly asStaticListarray can't grow.count(): Return the number of items in the list, supported on both storage types.at(index): Returns a const reference to the item at the specified index, supported on both storage types.operator[]: Returns a reference to the item at the specified index, supported on both storage types. Non-const access may trigger copy-on-write for shared storage.replace(index,newValue): Replaces the item at the specified index with a new item, supported on both storage types.clear(): Removes all nodes on theDynamicList's LinkedList, unsupported onStaticList.removeLast(): Removes the last item in the list, supported on DynamicList only.
See also list and QML Basic Types.
Member Function Documentation
ListProperty::ListProperty()
Creates a ListProperty without a specific List Storage type.
void ListProperty::append(T t)
Adds a new item to the end of the list and set it's value to t .
Note: This function is supported only with DynamicList storage.
typename IList<T>::const_reference ListProperty::at(int index) const
Returns the item at the specified index.
Use ListProperty::replace instead if you want to change a basic type.
bool ListProperty::canGrow() const
Returns true if the list storage supports growing (has the IListFlag_CanGrow flag set), otherwise returns false. Lists that can grow support operations like append() and resize().
void ListProperty::clear()
Removes all nodes on the DynamicList's LinkedList.
Note: This function is supported only with DynamicList storage.
int ListProperty::count() const
Returns the number of items in the list.
T ListProperty::get(int index) const
Returns the item at the specified index. This is equivalent to using the operator[] or at() functions.
Qul::IList<T> &ListProperty::getList() const
Returns a reference to the underlying list storage (IList implementation). This provides direct access to the list storage for advanced operations.
bool ListProperty::hasList() const
Returns true if the list has been initialized with a valid storage, otherwise returns false. A list without storage cannot be used for most operations.
template <typename U> void ListProperty::load(const Qul::ListProperty<U> &otherList)
Copies the items from otherList into this list, following same rules that are used with Qul::ListProperty::operator=.
bool ListProperty::removeAt(int index)
Removes the item at the specified index and returns true if successful, otherwise returns false.
Note: This function requires a list storage that supports item removal.
void ListProperty::removeLast()
Removes the last item in the list.
Note: This function is supported only with DynamicList storage.
void ListProperty::replace(int index, T newValue)
Sets the value of the item at the specified index to newValue.
void ListProperty::resize(int newSize)
Resizes the list to newSize. If the new size is smaller, items are removed from the end. If the new size is larger and the list can grow, new default-constructed items are appended.
Note: This function requires a list storage that supports the IListFlag_CanGrow flag.
void ListProperty::setList(Qul::IList<T> *l, bool silent = false)
Use l IList as the list's storage. The l parameter can be any implementation of IList, such as DynamicList or StaticList. Use silent to control whether the bindings to the list should be updated. Qt Quick Ultralite calls this function implicitly when initializing a list QML, and pick the correct storage type
See also hasList().
void ListProperty::syncList()
Synchronizes the length property with the current list count and emits the dataChanged signal with a reset notification. Use this function after manually modifying the underlying list storage.
template <typename L> Qul::ListProperty<T> &ListProperty::operator=(const L &otherList)
Load the items from otherList into L, which should also be a ListProperty.
The assignment operator uses the following rules depending on the size of the two lists, L and otherList, irrespective of their storage type:
- If the size of
Lis greater than the size ofotherList, items inLare replaced by items inotherListup to the size ofotherList. - If the size of
Lis less than the size ofotherList, items inLare replaced by items inotherListup to the size ofL. - If the size of
Lis equal to the size ofotherList, items inLare replaced by items inotherListup to the size ofL. - The type of the items in
otherListshould be the same as the type of the items inLlist.
The assignment operator is based on Qul::ListProperty::load
typename IList<T>::reference ListProperty::operator[](int index)
Returns a reference to the item at the specified index.
This triggers copy-on-write for shared list storage.
typename IList<T>::const_reference ListProperty::operator[](int index) const
Returns a const reference to the item at the specified index.
Use ListProperty::replace instead if you want to change a basic type.
Available under certain Qt licenses.
Find out more.