使用QQmlListProperty在Qml中显示和修改QList

时间:2013-10-09 23:28:24

标签: c++ qml qt5 qlist

再次,我有一个问题(也许是一个问题),我用qt和qml在qt5和qml用qtquick 2.0制作一个程序,我有一个c ++模型qlist,我需要在运行时修改列表,我使用q QQmlListProperty并显示qml中的项目,但是当我添加或删除我的代码时,它们不会隐藏和显示:

class ConceptsList: public QObject{ 

 Q_OBJECT
 Q_PROPERTY(QQmlListProperty<Concept> concepts READ concepts NOTIFY conceptsChanged) 
 Q_CLASSINFO("DefaultProperty", "concepts")

 public:
  ConceptsList(QObject *parent=0);

  QQmlListProperty<Concept> concepts();
  Q_INVOKABLE static void append_concept(QQmlListProperty<Concept> *list, Concept *cpt);

  Q_INVOKABLE void removeConcept(int index);
  Q_INVOKABLE void addConcept(QString m_id,QString description, QString quantity, QString price, QString unit, QString total);

  Q_INVOKABLE int countConcepts();

  static void clearConcepts(QQmlListProperty<Concept> *property);
  static int conceptsSize(QQmlListProperty<Concept> *property);
  static Concept *conceptAt(QQmlListProperty<Concept> *property, int index);

 signals:
  void conceptsChanged();

 private:
  QList<Concept *> m_concepts;
}

我使用listview和委托,我没有问题可以查看,但我的问题是,如果我可以使用QQmlListProperty并修改Qlist,或者我将更改表单以将qlist暴露给qml,如果它是可能的如何调用从qml的方法,或者如何在C ++中实现,我问,因为存在真正很少的数字或例子与这个形式的工作。 在qml我的代码是下一个:

    ConceptsList{
        id:cpts
        concepts:[
           Concept{
                m_id:"7"
                m_quantity: "3"
                m_price: "1"
                m_unit:"1"
                m_description:"algo"
                m_total:"2"
            }
        ]
    }

    ListView {
            id: listConceptsView
            objectName: "list"
            anchors.fill: parent
            anchors.margins: 5
            clip: true
            focus: true
            highlight: highlightBar
            highlightFollowsCurrentItem: false


            Component{
                id: tableConceptDelegate

                Item{
                    anchors.margins: 4
                    width: 515
                    height: 27
                    clip: true

                    Row {
                        spacing: 4

                        Text {
                            height: 26; width: 76
                            text: model.m_id
                            color: "black"
                            font.bold: true
                            horizontalAlignment: Text.AlignHCenter
                        }
                        ...

                        ...

                        Text {
                            height: 26; width: 120
                            text: model.m_total//amountTotal
                            color: "black"
                            font.bold: true
                            horizontalAlignment: Text.AlignHCenter
                        }
                    }

                    MouseArea {
                        id: mouse_area1
                        anchors.fill: parent
                        onClicked:
                        {
                            listConceptsView.currentIndex = index
                        }
                    }
                }

            }

            delegate: tableConceptDelegate
            model:cptCpt // i define this alias how cptCpt: cpt.concepts
        }

1 个答案:

答案 0 :(得分:5)

我得到了自己的答案,首先,我在方法append_concept中停止使用属性Q_INVOCABLE,其次,我在addConcept的实现中添加了一行代码。这是代码:

在:

Q_INVOKABLE static void append_concept(QQmlListProperty<Concept> *list, Concept *cpt);

现在:

static void append_concept(QQmlListProperty<Concept> *list, Concept *cpt);

也许这不会影响,但我不愿冒险。

addConceptremoveConcept的实施中:

 void ConceptsList::addConcept(QString m_id, QString quantity, QString price, QString unit, QString description)
 {
   Concept *cpt=new Concept(m_id, quantity, unit, price, description);

   m_concepts.append(cpt); 
   this->conceptsChanged();
 }

void ConceptsList::removeConcept(int index)
{
  m_concepts.removeAt(index);
  this->conceptsChanged();
}