Arrays

Due to the lazy copy mechanism, arrays differ from the standard access in javascript. Because the array is not copied into a QML compatible array container, access happens with methods.

Example: Instead of path.to.array[1].someproperty, you would write path.to.array.at(1).someproperty.

If you need the array as a javascript array, you can use toArray to copy the entire array and return it as a javascript array. The copy is only performed on the first call, subsequent calls should have less overhead.

API

class Array

View on an array field of a message. This allows access on array elements with lazy copy mechanism. Copies of an Array point to the same data and modifications of one array will be mirrored by the other.

Public Functions

QVariant at(int index) const

If the index is out of the bounds of the array, an empty QVariant is returned.

Parameters:

index – Index of the retrieved element.

Returns:

The array element at the given index.

void spliceList(int start, int delete_count, const QVariantList &items)

Changes the array content by removing delete_count elements at index and inserting the elements in items. This method can be used to remove, replace or add elements to the array.

Warning

If the operation is not limited to the end of the array, it requires a deep copy of the message array.

Parameters:
  • start – The index at which to start changing the array. If greater than the length of the array, start will be set to the length of the array. If negative, it will begin that many elements from the end of the array (with origin -1, meaning -n is the index of the nth last element and is therefore equivalent to the index of array.length - n). If the absolute value of start is greater than the length of the array, it will begin from index 0.

  • delete_count – The number of elements to delete starting at index start. If delete_count is greater than the number of elements after start, all elements from start to the length of the array are removed. If delete_count is 0, no elements are removed, e.g., for a insert only operation.

  • items – The items that will be inserted at start.

void push(const QVariant &value)

Adds the given value to the end of the array.

Parameters:

value – The item that is added.

void unshift(const QVariant &value)

Adds the given value to the front of the array.

Warning

This requires a deep copy of the message array on first call whereas appending can be done without copying the array.

Parameters:

value – The item that is added.

QVariant pop()

Removes the last element and returns it.

Returns:

The removed element or an empty QVariant if the array is empty.

QVariant shift()

Removes the first element and returns it.

Warning

This requires a deep copy of the message array on first call whereas appending can be done without copying the array.

Returns:

The removed element or an empty QVariant if the array is empty.

QVariantList toArray()

Converts the array to a QVariantList which can be used in place of a JS array in QML. This method performs a deep copy of the message array on the first call.

Returns:

The array as a QVariantList.

Properties

int length

The length of the array, i.e., the number of elements.