热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

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

如何解决《如何通过QQmlListProperty将listmodel分配给QML列表视图》经验,为你挑选了1个好方法。

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

班级代码:

class TreeModel : public QAbstractItemModel
{
    Q_OBJECT
    Q_PROPERTY(QQmlListProperty 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 lists(){
            return QQmlListProperty(this, foldermodels);
    }


    ********************************************
    ListModel *groupmodel;
    QList foldermodels;
    QList filemodels;

现在我如何分配例如foldermodels.at(0)到qml中的listview?我试过像这样的东西:

 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' 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' for property 'TreeModel::lists'
QQmlExpression: Expression qrc:/main.qml:54:28 depends on non-NOTIFYable properties:
    TreeModel::lists

是的,我已经注册了包含QList的Treemodel类.

我也知道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 >("ListMode",1,0,"listmod");

现在我有2个新错误:

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


C:\Qt\5.4\mingw491_32\include\QtQml\qqml.h:244: error: 'staticMetaObject' is not a member of 'QQmlListProperty'
         uri, versionMajor, versionMinor, qmlName, &T::staticMetaObject,

CantThinkOfA.. 5

在我的main.cpp中,我添加了一行:

qmlRegisterType();

现在它工作,我能够通过QQmlListProperty在QML中使用ListModel对象

代码如下:

treemodel.cpp

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

    QQmlListProperty folderLists(){
        return QQmlListProperty(this, foldermodels);
    }


    QList foldermodels;
}

main.cpp中

TreeModel model;

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

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

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

return app.exec();

哦,为了完整起见,

在qml中你可以调用listmodel,如:

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函数返回一个用于迭代QList列表的整数.

希望这可以帮助别人



1> CantThinkOfA..:

在我的main.cpp中,我添加了一行:

qmlRegisterType();

现在它工作,我能够通过QQmlListProperty在QML中使用ListModel对象

代码如下:

treemodel.cpp

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

    QQmlListProperty folderLists(){
        return QQmlListProperty(this, foldermodels);
    }


    QList foldermodels;
}

main.cpp中

TreeModel model;

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

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

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

return app.exec();

哦,为了完整起见,

在qml中你可以调用listmodel,如:

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函数返回一个用于迭代QList列表的整数.

希望这可以帮助别人


推荐阅读
author-avatar
C3calm_daidai_649
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有