本页内容

C

Qul::ListProperty Struct

template <typename T> struct Qul::ListProperty

使用 ListProperty 类来存储指定类型的列表。更多内容...

Header: #include <qul/listproperty.h>
Since: Qt Quick Ultralite 2.9

公共函数

ListProperty()
void append(T t)
T at(int index)
void clear()
int count() const
bool isDynamic() const
bool isNull() const
bool isStatic() const
Qul::ListProperty<T> &load(const L &otherList)
void removeLast()
void replace(int index, T newValue)
void setList(Qul::DynamicList<T> *l)
void setList(Qul::StaticList<T> *l)
Qul::ListProperty<T> &operator=(const L &otherList)
T operator[](int index) const

详细说明

将此类用作 C++ 对象的公共成员,这些成员在 QML 中将作为该对象的属性列表呈现。该类的模板参数定义了 C++ 类型,并映射到相应的 QML 类型。

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);
    }
};

您可以在 QML 绑定中使用这些 C++ 属性列表。

如下例所示,可以将 JavaScript 数组赋值给列表

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 }
    ]
}

还可以在“分组属性”中声明列表

struct MyObject : public Qul::Object
{
    struct Grouped
    {
        Qul::Property<int> val1;
        Qul::Property<int> val2;
        Qul::ListProperty<int> list1;
    };
    Grouped group;
};

列表存储

在QUL 中,可以根据列表是否需要动态扩展,在两种存储类型之间进行选择。

您可以在Qul::ListProperty 的cpp文件中,通过使用setList 函数显式设置存储方式,如下所示here

DynamicList

DynamicList 是QML列表在未指定特殊关键字时的默认存储类型。Qt Quick Ultralite使用链表将项目存储在堆中。仅当您确定列表需要扩展时才使用DynamicList ,否则请改用static_list 。

Qul::ListProperty<int> list;
list.setList(new Qul::DynamicList<int>());

StaticList

为避免堆内存分配,您可以改用StaticList 存储方式。StaticList 仅需数组指针及其大小即可静态分配内存,从而减少运行时的内存开销。

//... declaration
Qul::ListProperty<int> list;
int data[10];
Qul::StaticList<int> storage;
//... initialization
storage.size = 10;
storage.data = data;
list.setList(storage);

StaticListFixed

由于声明数组并同时声明一个StaticList ,然后对两者进行初始化,是一种常见的模式。您可以使用StaticListFixed (继承自StaticList )来简化这一过程,从而将其用作ListProperty的存储。

与依赖于其自身未管理的外部指针的StaticList 不同,StaticListFixed 会在内部管理数组,并在被销毁时清理该数组。

// ... 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);

支持的操作

ListProperty 支持以下操作。根据所使用的存储类型,某些操作可能无法执行,因此在使用这些受限函数之前,必须设置正确的存储类型。

  • append(): 将新项目添加到列表末尾,仅在DynamicList 上受支持,因为StaticList 数组无法扩展。
  • count(): 返回列表中的项目数量,两种存储类型均支持。
  • at(index): 返回指定索引处的项,在两种存储类型上均受支持。
  • operator[]: 返回指定索引处的项,两种存储类型均支持。与at 不同,它返回的是一个常量项。
  • replace(index,newValue): 将指定索引处的元素替换为新元素,这在两种存储类型上均受支持。
  • clear(): 移除DynamicList 的 LinkedList 中的所有节点,StaticList 不支持此操作。
  • removeLast(): 移除列表中的最后一个元素,仅在DynamicList 上受支持。

另请参阅 list 和QML 基本类型。

成员函数文档

ListProperty::ListProperty()

创建一个未指定具体List Storage 类型的ListProperty。

void ListProperty::append(T t)

将一个新项添加到列表末尾,并将其值设置为t 。

注意:此 函数仅支持DynamicList 存储。

T ListProperty::at(int index)

返回位于指定index 处的项。

若要修改基本类型,请改用 `ListProperty::replace `。

void ListProperty::clear()

删除DynamicList 的LinkedList中的所有节点。

注意:此 函数仅在DynamicList 存储中受支持。

int ListProperty::count() const

返回列表中的项目数量。

bool ListProperty::isDynamic() const

如果列表使用的是DynamicList 存储,则返回true ;否则返回false 。

bool ListProperty::isNull() const

如果列表未初始化,则返回true ;否则返回false 。

bool ListProperty::isStatic() const

如果列表使用的是StaticList 存储,则返回true ;否则返回false 。

template <typename L> Qul::ListProperty<T> &ListProperty::load(const L &otherList)

将otherList 中的项目复制到L 列表中,遵循与Qul::ListProperty::operator= 相同的规则。

void ListProperty::removeLast()

删除列表中的最后一个项目。

注意:此 函数仅支持DynamicList 存储。

void ListProperty::replace(int index, T newValue)

将指定index 处的项的值设置为newValue 。

void ListProperty::setList(Qul::DynamicList<T> *l)

使用l DynamicList 作为列表的存储。Qt Quick Ultralite 在QML 中初始化list 时会隐式调用此函数。

void ListProperty::setList(Qul::StaticList<T> *l)

使用l StaticList 作为列表的存储空间。Qt Quick 当在QML 中使用static_list 或readonly 关键字初始化列表时,Ultralite 会隐式调用此函数。

template <typename L> Qul::ListProperty<T> &ListProperty::operator=(const L &otherList)

将otherList 中的项目加载到L 中,后者也应为ListProperty 类型。

赋值运算符根据两个列表L 和otherList 的大小遵循以下规则,其存储类型无关紧要:

  • 如果L 的大小大于otherList 的大小,则L 中的项将被otherList 中的项替换,替换数量上限为otherList 的大小。
  • 如果L 的大小小于otherList 的大小,则L 中的项目将被otherList 中的项目替换,替换数量不超过L 的大小。
  • 如果L 的大小等于otherList 的大小,则L 中的项将被otherList 中的项替换,替换数量不超过L 的大小。
  • otherList 中项的类型应与L 列表中项的类型相同。

赋值运算符基于Qul::ListProperty::load

T ListProperty::operator[](int index) const

返回位于指定index 处的项。

若要修改基本类型,请改用ListProperty::replace 。

在某些 Qt 许可下可用。
了解更多。