gpt4 book ai didi

c++ - 如何通过 QQmlListProperty 将列表模型分配给 QML ListView

转载 作者:太空狗 更新时间:2023-10-29 20:24:18 27 4
gpt4 key购买 nike

如果我有一个包含列表模型 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 中这样做时 View 会显示项目

    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,

最佳答案

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

qmlRegisterType<ListModel>();

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

代码如下:

树模型.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;
}

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

希望对大家有所帮助

关于c++ - 如何通过 QQmlListProperty 将列表模型分配给 QML ListView ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28695370/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com