QQist

2013-10-09 18 views
5

'da QList'i göstermek ve değiştirmek için QQmlListProperty'yi kullanın, bir sorum var (ve belki de bir sorun), qt ve qml qt ve qml qtquick 2.0 ile bir program yaparım ve bir C++ model listesi var.QQist

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; 
} 

Ben kullanın: ve ben q QQmlListProperty kullanmak ve QML öğeleri gösterebilir, ancak gizlemek ve göstermek an ı eklediğinizde ya da benim kod yanındadır uzaklaştırırlar, çalışma zamanı içinde listesinde değişiklik gerek liste görünümü ve temsilci ve görüntülemek için sorun yok, ancak bir QQmlListProperty kullanabilir ve Qlist'i değiştirebilir miyim, yoksa qml için qlist'e nasıl görüneceğini bir form olarak değiştiririm, eğer qml yöntemini nasıl çağırırsam, veya nasıl C++ 'da uygulamak, ben gerçekten çok az sayı veya iş th ile örnekler var çünkü sor formudur. QML benim kod yanında:

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 
     } 

cevap

5

Ben ilk ben, yöntem append_concept öznitelik Q_INVOCABLE kullanılarak durduruldu ikinci, ben addConcept uygulanmasında kod satırını eklendi, benim kendi için cevabı var.

önce:: Artık

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

: İşte kod

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

Belki bu etkilemez, ama risk almayın tercih ediyoruz.

Ve addConcept ve removeConcept uygulamalarında

: bu, sadece yorum olarak

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

maruz listesi (bu liste isteği olsun bir http tamamen değişti örneğin) veya doktorunun tüm her zaman değiştiğinde Program akışı için sonuçları bir kez daha listeden yükleyiniz. – APRocha

+0

append_concept (QQmlListProperty * liste, Kavram * cpt) bir hata atmalı, QQmlListProperty türünde olmamalıdır? –