<bdo id='C1sTx'></bdo><ul id='C1sTx'></ul>
<legend id='C1sTx'><style id='C1sTx'><dir id='C1sTx'><q id='C1sTx'></q></dir></style></legend>

    1. <small id='C1sTx'></small><noframes id='C1sTx'>

        <i id='C1sTx'><tr id='C1sTx'><dt id='C1sTx'><q id='C1sTx'><span id='C1sTx'><b id='C1sTx'><form id='C1sTx'><ins id='C1sTx'></ins><ul id='C1sTx'></ul><sub id='C1sTx'></sub></form><legend id='C1sTx'></legend><bdo id='C1sTx'><pre id='C1sTx'><center id='C1sTx'></center></pre></bdo></b><th id='C1sTx'></th></span></q></dt></tr></i><div id='C1sTx'><tfoot id='C1sTx'></tfoot><dl id='C1sTx'><fieldset id='C1sTx'></fieldset></dl></div>

      1. <tfoot id='C1sTx'></tfoot>
      2. qml 中的嵌套列表:模型中的数据模型

        Nested list in qml: data models in models(qml 中的嵌套列表:模型中的数据模型)
              <tbody id='azFSr'></tbody>
              <bdo id='azFSr'></bdo><ul id='azFSr'></ul>

              <small id='azFSr'></small><noframes id='azFSr'>

              • <legend id='azFSr'><style id='azFSr'><dir id='azFSr'><q id='azFSr'></q></dir></style></legend>
                <i id='azFSr'><tr id='azFSr'><dt id='azFSr'><q id='azFSr'><span id='azFSr'><b id='azFSr'><form id='azFSr'><ins id='azFSr'></ins><ul id='azFSr'></ul><sub id='azFSr'></sub></form><legend id='azFSr'></legend><bdo id='azFSr'><pre id='azFSr'><center id='azFSr'></center></pre></bdo></b><th id='azFSr'></th></span></q></dt></tr></i><div id='azFSr'><tfoot id='azFSr'></tfoot><dl id='azFSr'><fieldset id='azFSr'></fieldset></dl></div>
                <tfoot id='azFSr'></tfoot>

                1. 本文介绍了qml 中的嵌套列表:模型中的数据模型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我正在尝试在 QML 界面中实现嵌套评论系统.我有一个 C++ 模型(从 QAbstractListModel 子类化),其中模型中的每个项目都返回两个值:一个是 QString,另一个是角色名称为dataMap"的 QVariantMap.这适用于 QML ListView.现在每个 QVariantMap 包含一个项目data",它进一步包含一个 QVariantListchildren".现在这基本上列出了具有相同结构的其他 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;
                  

                  现在这种方法由于多种原因不起作用(其中之一是属性 dataMap 可以作为 childModel 中的 data 访问>,它被任何 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?

                  推荐答案

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

                  I have found this very useful article that helped to solve the problem https://lemirep.wordpress.com/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 中的嵌套列表:模型中的数据模型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  Is Type(::x); valid?(是类型(::x);有效的?)
                  Difference between an inline function and static inline function(内联函数和静态内联函数的区别)
                  Compilation fails randomly: quot;cannot open program databasequot;(编译随机失败:“无法打开程序数据库)
                  Too many initializers error for a simple array in bcc32(bcc32 中的简单数组的初始值设定项过多错误)
                  No Member named stoi in namespace std(命名空间 std 中没有名为 stoi 的成员)
                  Error using a constexpr as a template parameter within the same class(在同一个类中使用 constexpr 作为模板参数时出错)
                    <bdo id='fjgAW'></bdo><ul id='fjgAW'></ul>
                    <legend id='fjgAW'><style id='fjgAW'><dir id='fjgAW'><q id='fjgAW'></q></dir></style></legend>
                    <i id='fjgAW'><tr id='fjgAW'><dt id='fjgAW'><q id='fjgAW'><span id='fjgAW'><b id='fjgAW'><form id='fjgAW'><ins id='fjgAW'></ins><ul id='fjgAW'></ul><sub id='fjgAW'></sub></form><legend id='fjgAW'></legend><bdo id='fjgAW'><pre id='fjgAW'><center id='fjgAW'></center></pre></bdo></b><th id='fjgAW'></th></span></q></dt></tr></i><div id='fjgAW'><tfoot id='fjgAW'></tfoot><dl id='fjgAW'><fieldset id='fjgAW'></fieldset></dl></div>

                      <small id='fjgAW'></small><noframes id='fjgAW'>

                      <tfoot id='fjgAW'></tfoot>

                            <tbody id='fjgAW'></tbody>