QML中的嵌套列表:模型中的数据模型

编程入门 行业动态 更新时间:2024-10-27 18:19:17
本文介绍了QML中的嵌套列表:模型中的数据模型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在尝试在QML界面中实现嵌套评论系统.我在C ++中有一个模型(从QAbstractListModel继承),其中模型中的每个项目返回两个值:一个是QString,另一个是带有RoleName"dataMap"的QVariantMap.使用QML ListView可以很好地工作.现在,每个QVariantMap包含一个项数据",该项还包含一个QVariantList子项".现在,这基本上列出了其他具有相同结构的QVariantMap.我实现此目标的想法是在QML ListView中使用递归委托.下面是我的代码的最简单版本.

I am trying to implement a nested comment system in a QML interface. I have a model in C++ (subclassed from QAbstractListModel) in which each item in the model returns two values: one is a QString and the other is a QVariantMap with roleName "dataMap". This works fine with a QML ListView. Now each QVariantMap contains an item "data" which further contains a QVariantList "children". Now this lists basically other QVariantMaps with the same structure. My idea to implement this was to use a recursive delegate in a QML ListView. Below is the simplest version of my code.

ListView{ id: commentsList anchors.fill: parent model: commentsModel delegate: commentsDelegate } Component{ id: commentsDelegate ColumnLayout{ Rectangle{ width: 600 height: 200 Text { id: bodyText text: dataMap.body anchors.centerIn: parent Component.onCompleted: console.debug(text) } } ListView{ id: childList property var childModel: dataMap.replies.data.children // QVariantList exposed to QML x: 15 interactive: false model: childModel anchors.fill: parent delegate: commentsDelegate } } }

我的模型的结构如下:

class ListModel : public QAbstractListModel { Q_OBJECT public: ListModel(){} explicit ListModel(QObject* parent =0); ~ListModel(); QHash<int, QByteArray> roleNames() const; QVariant data(const QModelIndex & index, int role) const; int rowCount(const QModelIndex &parent) const; void addItem(ListItem item); void clearModel(); private: QList<ListItem> m_itemsList; signals: void dataChanged(const QModelIndex & topLeft, const QModelIndex & bottomRight); };

ListItem类就是

The ListItem class is simply

class ListItem { public: ListItem(QObject* parent = 0) : QObject(parent) {} virtual ~ListItem() {} ListItem(const QString & type, const QVariantMap & dataMap); QString type() const; QVariantMap dataMap() const; private: QString m_type; QVariantMap m_dataMap;

现在,由于多种原因,该方法不起作用(其中之一是可以在 childModel ,在任何QML项类型中,默认属性 data 都将其覆盖).这个问题有什么可能的解决办法吗?

Now this approach does not work for a number of reasons (one of which is that the property dataMap is accessible as data in the childModel, which is overridden by the default property data in any QML Item type). Any possible solution to this problem?

推荐答案

我发现这篇非常有用的文章有助于解决问题 lemirep.wordpress/2013/04/06/a-practical-case-exposed- qt-c-models-to-qml/. 该方法包括在模型类内部创建另一个ListModel(派生自QAbstracListModel).在我的示例中,我将QVariantMap dataMap()替换为另一个ListModel dataModel().请注意,这也需要其他更改(可以在提供的链接中找到)

I have found this very useful article that helped to solve the problem lemirep.wordpress/2013/04/06/a-practical-case-exposing-qt-c-models-to-qml/. The approach consists into creating another ListModel (derived from QAbstracListModel) inside the model class. In my example, I replace QVariantMap dataMap() with another ListModel dataModel(). Notice that this requires other changes too (which can be found at the link provided)

更多推荐

QML中的嵌套列表:模型中的数据模型

本文发布于:2023-11-04 05:53:08,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1557128.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:嵌套   模型   数据模型   列表   QML

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!