如果我们想在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();
}
}
在将类暴露给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
}
}
我可以使用将项目添加到列表中或清除它的功能吗?如上所示,我可以使用CountFunction
和AtFunction
使用.length
和ClearFunction
。我可以以某种方式使用AppendFunction
和QQmlListProperty
吗?
也许我不能用QAbstractListModel
执行此操作,我应该使用SELECT a.uidsgn, a.uname, a.ubatid, a.uloc, a.dpcodu , a.smbrch
FROM Fefusr AS a
LEFT JOIN Ssfuser AS b ON a.uidsgn = b.ssuidsgn
LEFT JOIN Ssfgroup AS c ON b.ssgrpid = c.ssgrpid
LEFT JOIN Ssfuserchg AS d ON a.uidsgn = d.ssuidsgn AND d.statusaut <> '1'
AND d.ssidmenu = 'LOC'
WHERE a.smbrch = '1' AND b.ssuidsgn <> 'BK20151101'
AND a.nextp NOT IN('JWPLOGON, ATM01C')
ORDER BY d.ssuidsgn
?
答案 0 :(得分:6)
编辑:由于列表属性界面发生了变化,下面的答案不再完全正确。现在可以使用push
(来自下面链接的文档)附加对象:
可以使用push方法将值动态添加到列表中,就好像它是一个JavaScript数组
下面的原始答案
您无法直接在QML中附加元素或清除QQmlListProperty
。要在QML中编辑QQmlListProperty
,请为其指定一个新列表。
根据QML list
documentation:
从C ++传递到QML的任何
QQmlListProperty
值都会自动转换为list
值,反之亦然。请注意,创建后无法将对象单独添加到列表中或从列表中删除;要修改列表的内容,必须将其重新分配给新列表。