如何通过 QQmlListProperty 将列表模型分配给 QML 列表视图

how to assign a listmodel to a QML listview through the QQmlListProperty

如果我有一个 class 包含列表模型的 QList (QList),我将如何将该列表中的模型分配给 QML 中的列表视图?

class代码:

class TreeModel : public QAbstractItemModel
{
    Q_OBJECT
    Q_PROPERTY(QQmlListProperty<ListModel> lists READ lists)

public:

    enum AnimalRoles {
        TypeRole = Qt::UserRole + 1,
    };
    explicit TreeModel(const QString &data, QObject *parent = 0);
    ~TreeModel();

    QVariant data(const QModelIndex &index, int role) const Q_DECL_OVERRIDE;
    Qt::ItemFlags flags(const QModelIndex &index) const Q_DECL_OVERRIDE;
    QVariant headerData(int section, Qt::Orientation orientation,
                        int role = Qt::DisplayRole) const Q_DECL_OVERRIDE;
    QModelIndex index(int row, int column,
                      const QModelIndex &parent = QModelIndex()) const Q_DECL_OVERRIDE;
    QModelIndex parent(const QModelIndex &index) const Q_DECL_OVERRIDE;
    int rowCount(const QModelIndex &parent = QModelIndex()) const Q_DECL_OVERRIDE;
    int columnCount(const QModelIndex &parent = QModelIndex()) const Q_DECL_OVERRIDE;

    QStringList getGroups();
    QStringList getFoldersByGroup(const QString &name);
    QStringList getFolderlistByGroupID(QStringList &name);
    void getFolders();
    void populateFrontPage();

    QQmlListProperty<ListModel> lists(){
            return QQmlListProperty<ListModel>(this, foldermodels);
    }


    ********************************************
    ListModel *groupmodel;
    QList<ListModel*> foldermodels;
    QList<ListModel*> filemodels;

现在我如何将 foldermodels.at(0) 分配给 qml 中的列表视图? 我尝试过类似的东西:

 ListView {
 id: folderlist
  model: treemodel.lists // treemodel.lists.at(0) // treemodel.lists[0]
  delegate: folderDelegate
  contentHeight: contentItem.childrenRect.height
  height: childrenRect.height
  anchors.left: parent.left
  anchors.right: parent.right
  clip: true
}

但我收到如下错误:

QMetaProperty::read: Unable to handle unregistered datatype 'QQmlListProperty<ListModel>' for property 'TreeModel::lists'
QQmlExpression: Expression qrc:/main.qml:54:28 depends on non-NOTIFYable properties:
    TreeModel::lists
QMetaProperty::read: Unable to handle unregistered datatype 'QQmlListProperty<ListModel>' for property 'TreeModel::lists'
QQmlExpression: Expression qrc:/main.qml:54:28 depends on non-NOTIFYable properties:
    TreeModel::lists

是的,我已经注册了包含 QList 的树模型 class。

我也知道 QList 实际上填充了正确的模型,因为当我在 main.cpp

中这样做时,视图会显示项目
    TreeModel model(infile.readAll());
    ListModel *foldermodel = model.foldermodels.at(1) // or (0)
    ctxt->setContextProperty("treemodel", &model);
    ctxt->setContextProperty("foldermodel", foldermodel);

在此先感谢您的帮助,我真的很感激!!

更多进度:

我将此行添加到我的 main.cpp

qmlRegisterType<QQmlListProperty<ListModel> >("ListMode",1,0,"listmod");

现在我有 2 个新错误:

C:\Qt.4\mingw491_32\include\QtQml\qqml.h:83: error: 'staticMetaObject' is not a member of 'QQmlListProperty<ListModel>'
     const char *className = T::staticMetaObject.className(); \


C:\Qt.4\mingw491_32\include\QtQml\qqml.h:244: error: 'staticMetaObject' is not a member of 'QQmlListProperty<ListModel>'
         uri, versionMajor, versionMinor, qmlName, &T::staticMetaObject,
Unable to handle unregistered datatype 'QQmlListProperty<ListModel>

这意味着 QML 不知道 属性 的类型。您需要使用 Q_DECLARE_METATYPE:

注册它
Q_DECLARE_METATYPE(QQmlListProperty<ListModel>)

在我的 main.cpp 中添加了以下行:

qmlRegisterType<ListModel>();

现在可以使用了,我可以通过 QQmlListProperty 在 QML 中使用 ListModel 对象

代码如下:

treemodel.cpp

class ListModel
class TreeModel : public QAbstractItemModel
{
    Q_OBJECT
    Q_PROPERTY(QQmlListProperty<ListModel> folderLists READ folderLists)
    Q_PROPERTY(QQmlListProperty<ListModel> fileLists READ fileLists)
    Q_PROPERTY(QQmlListProperty<ListModel> getFileAttributes READ getFileAttributes)
    Q_PROPERTY(QQmlListProperty<ListModel> getFileUserAndDate READ getFileUserAndDate)
...

    QQmlListProperty<ListModel> folderLists(){
        return QQmlListProperty<ListModel>(this, foldermodels);
    }


    QList<ListModel*> foldermodels;
}

main.cpp

TreeModel model;

QQuickView view;
view.setResizeMode(QQuickView::SizeRootObjectToView);
QQmlContext *ctxt = view.rootContext();

qmlRegisterType<ListModel>();
ctxt->setContextProperty("treemodel", &model);

view.setSource(QUrl(QStringLiteral("qrc:/main.qml")));
view.show();

return app.exec();

哦,为了完整起见,

在 qml 中你可以这样调用列表模型:

ListView {
   id: folderlist
   model: treemodel.folderLists[treemodel.modIndex]
   delegate: folderDelegate
   contentHeight: contentItem.childrenRect.height
   height: childrenRect.height
   anchors.left: parent.left
   anchors.right: parent.right
   clip: true
   spacing: 3
}

modIndex 函数returns 一个用于迭代 QList 列表的整数。

希望这对某人有所帮助