Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use QQmlListProperty to show and modify QList in Qml

Tags:

c++

qt5

qml

qlist

again, well i have a question (and maybe a problem), i make a program with qt and qml in qt5 and qml with qtquick 2.0, and i have a c++ model qlist, and i need modify the list in runtime, i use q QQmlListProperty and show the items in qml, but they are not hide and show in the moment when i add or remove my code is next:

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

I use a listview and delegate and i not have problems to view, but my question is if i can use a QQmlListProperty and modify the Qlist, or i will change a form to expose qlist to qml, if it's posible how call the method from qml, or how implement in C++, i ask because exists really little number or examples with work this form. in qml my code is next:

    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
        }
like image 340
APRocha Avatar asked Oct 09 '13 23:10

APRocha


1 Answers

I got the answer for my self, first, I stopped using the attribute Q_INVOCABLE in the method append_concept, second, I added a line of code in the implementation of addConcept. Here is the code:

Before:

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

Now:

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

Maybe this not affect, but i prefer not take risks.

And in the implementations of addConcept and removeConcept:

 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();
}
like image 88
APRocha Avatar answered Nov 06 '22 15:11

APRocha