如何在 QML 中编辑 QQmlListProperty

How to edit QQmlListProperty in QML

如果我们想在 QML 中使用自定义 C++ 对象列表,我们可以使用 QQmlListProperty

在 QML 中注册时,我们需要指定一个函数,QML 将使用该函数读取列表。

文档指出,对于功能齐全的列表,我们需要使用此函数:

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

这是我如何用 C++ 编写它的示例:

classwithlist.h

#ifndef CLASSWITHLIST_H
#define CLASSWITHLIST_H

#include <QObject>
#include <QQmlListProperty>
#include "element.h"

class Element;

class ClassWithList : public QObject
{
    Q_OBJECT
    Q_PROPERTY(QQmlListProperty<Element> elements  READ getElements  NOTIFY elementsChanged)


public:
    explicit ClassWithList(QObject *parent = 0);

    QQmlListProperty<Element> getElements();
    void appendElements(QQmlListProperty<Element> *list, Element *e);
    static int elementsCount(QQmlListProperty<Element> *list);
    static Element* elementsAt(QQmlListProperty<Element> *list, int i);
    static void elementsClear(QQmlListProperty<Element> *list);

signals:
    void elementsChanged(QQmlListProperty<Element>);

private:
    QList<Element *> m_elements;

};

#endif // CLASSWITHLIST_H

classwithlist.cpp

#include "classwithlist.h"

ClassWithList::ClassWithList(QObject *parent) : QObject(parent)
{

}

QQmlListProperty<Element> ClassWithList::getElements()
{
    return QQmlListProperty<Element>(this, m_elements, &appendElements,&elementsCount,&elementsAt,&elementsClear);
}
void ClassWithList::appendElements(QQmlListProperty<Element> *list, Element *e)
{
    ClassWithList *cwl = qobject_cast<ClassWithList*>(list->object);
    if (cwl && e) {
        cwl->m_elements.append(e);
    }
}
int ClassWithList::elementsCount(QQmlListProperty<Element> *list)
{
    ClassWithList *cwl = qobject_cast<ClassWithList*>(list->object);
    if (cwl)
        return cwl->m_elements.count();
    return 0;
}
Element *ClassWithList::elementsAt(QQmlListProperty<Element> *list, int i)
{
    ClassWithList *cwl = qobject_cast<ClassWithList*>(list->object);
    if (cwl)
        return cwl->m_elements.at(i);
    return 0;
}
void ClassWithList::elementsClear(QQmlListProperty<Element> *list)
{
    ClassWithList *cwl = qobject_cast<ClassWithList*>(list->object);
    if (cwl) {
        cwl->m_elements.clear();
    }
}

在将 class 暴露给 QML 后,我在 QML 中有以下代码

ClassWithList {
    Component.onCompleted: {
        console.log(elements.length) // works
        console.log(elements[0])     // works

//        elements.push()      // does not work
//        elements.append()    // does not work
//        elements.clear()     // does not work
//        elements.at()        // does not work
    }
}

我可以使用将项目添加到列表或清除它的函数吗?如上所示,我可以通过使用 .length 和括号来使用函数 CountFunctionAtFunction。我也可以使用 ClearFunctionAppendFunction 吗?

也许我不能用 QQmlListProperty 做到这一点,我应该使用 QAbstractListModel?

编辑: 由于列表 属性 界面的变化,下面的答案不再完全正确。现在可以使用 push 附加对象(来自下面链接的文档):

Values can be dynamically added to the list by using the push method, as if it were a JavaScript Array

下面是原回答


您不能在 QML 中直接向 QQmlListProperty 添加元素或清除 QQmlListProperty。要在 QML 中编辑 QQmlListProperty,请为其分配一个新列表。

根据 QML list documentation:

Any QQmlListProperty value passed into QML from C++ is automatically converted into a list value, and vice-versa.

Note that objects cannot be individually added to or removed from the list once created; to modify the contents of a list, it must be reassigned to a new list.