作者:C3calm_daidai_649 | 来源:互联网 | 2023-05-20 16:47
如果我有一个包含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列表的整数.
希望这可以帮助别人