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

时间:2015-02-24 12:05:57

标签: c++ qt listview qml

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

班级代码:

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? 我尝试过这样的东西:

 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的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<QQmlListProperty<ListModel> >("ListMode",1,0,"listmod");

现在我有2个新错误:

C:\Qt\5.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\5.4\mingw491_32\include\QtQml\qqml.h:244: error: 'staticMetaObject' is not a member of 'QQmlListProperty<ListModel>'
         uri, versionMajor, versionMinor, qmlName, &T::staticMetaObject,

2 个答案:

答案 0 :(得分:5)

在我的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中你可以调用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 :(得分:1)

Unable to handle unregistered datatype 'QQmlListProperty<ListModel>

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

Q_DECLARE_METATYPE(QQmlListProperty<ListModel>)