2013-10-09 38 views
5

中再次显示和修改QList,以及我有一个问题(也许是一个问题),我使用qt和qml在qt5和qml中使用qtquick 2.0创建程序,并且我有一个C++模型qlist,我需要修改运行列表中,我使用q QQmlListProperty并显示在QML的项目,但它们不是隐藏和显示在那一刻,我添加或删除我的代码是未来:使用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 
     } 

回答

5

我得到了答案我自己,首先,我停止使用该属性Q_INVOCABLE的方法append_concept,第二,我添加了一行代码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(); 
} 
+0

为注释这仅适用于当暴露列表中改变了一切每次(例如为此列表中的HTTP一切都改变get请求),或者是更多钞票重新加载所有列表,而不会对程序流程造成影响。 – APRocha

+0

append_concept(QQmlListProperty * list,Concept * cpt)应该抛出一个错误,不应该是QQmlListProperty类型? –