title: QML dynamic ComboBox Entrys
body: <p>i'm trying to implement a ComboBox in QML and C++, in which i can dynamically change the content of my ComboBox. I'm providing a QStringList as ComboBox-Model and want to insert/remove an entry from the ComboBox with two buttons. </p>
  
  <p>I can display the QStringList in my ComboBox, but when i click on the Add-Button for example, my QStringList gets updated in my class, but somehow the View does not update the ComboBox entrys. What should i do, to tell the View that my ComboBox-Model has updated?</p>
  
  <p>This is my code:</p>
  
  <p><strong>comboboxmodel.h</strong></p>
  
  <pre><code>#ifndef COMBOBOXMODEL_H
  #define COMBOBOXMODEL_H
  
  #include &lt;QObject&gt;
  #include &lt;QStringList&gt;
  
  class ComboBoxModel : public QObject
  {
      Q_OBJECT
  
      Q_PROPERTY(QStringList comboList READ comboList WRITE setComboList NOTIFY comboListChanged)
      Q_PROPERTY(int count READ count WRITE setCount NOTIFY countChanged)
  
  public:
      ComboBoxModel(QObject *parent = 0);
      ComboBoxModel(const QStringList &amp;list,int count, QObject *parent = 0);
  
      const QStringList comboList();
      void setComboList(const QStringList &amp;comboList);
  
      int count();
      void setCount(int cnt);
  
      Q_INVOKABLE void addElement(const QString &amp;element);
      Q_INVOKABLE void removeElement(int index);
  
  signals:
  
      void comboListChanged();
      void countChanged();
  
  public slots:
  
  
  private:
  
      QStringList m_comboList;
      int         m_count;
  };
  
  #endif // COMBOBOXMODEL_H
  </code></pre>
  
  <p><strong>comboboxmodel.cpp</strong></p>
  
  <pre><code>#include "comboboxmodel.h"
  #include "qdebug.h"
  
  ComboBoxModel::ComboBoxModel(QObject *parent) :
      QObject(parent)
  {
  }
  
  ComboBoxModel::ComboBoxModel(const QStringList &amp;list, int count, QObject *parent) :
      QObject(parent), m_comboList(list), m_count(count)
  {
  
  }
  
  const QStringList ComboBoxModel::comboList()
  {
      return m_comboList;
  }
  
  void ComboBoxModel::setComboList(const QStringList &amp;comboList)
  {
  
      if (m_comboList != comboList)
      {
          m_comboList = comboList;
          emit comboListChanged();
      }
  
  }
  
  int ComboBoxModel::count()
  {
      return m_count;
  }
  
  void ComboBoxModel::setCount(int cnt)
  {
      if (cnt != m_count)
      {
          m_count = cnt;
          emit countChanged();
      }
  }
  
  void ComboBoxModel::addElement(const QString &amp;element)
  {
      m_comboList.append(element);
      emit comboListChanged();
      setCount(m_comboList.count());
      emit countChanged();
  
      for (int i = 0; i&lt;m_count; i++)
      {
          qDebug() &lt;&lt; m_comboList.at(i);
      }
  }
  
  void ComboBoxModel::removeElement(int index)
  {
      if (index &lt; m_comboList.count())
      {
          m_comboList.removeAt(index);
          emit comboListChanged();
          setCount(m_comboList.count());
          emit countChanged();
      }
  
      for (int i = 0; i&lt;m_count; i++)
      {
          qDebug() &lt;&lt; m_comboList.at(i);
      }
  }
  </code></pre>
  
  <p><strong>main.cpp</strong></p>
  
  <pre><code>#include &lt;QApplication&gt;
  #include &lt;QQmlApplicationEngine&gt;
  #include &lt;qqmlengine.h&gt;
  #include &lt;qqmlcontext.h&gt;
  #include &lt;qqml.h&gt;
  #include &lt;QStringList&gt;
  
  #include "comboboxmodel.h"
  
  int main(int argc, char *argv[]) {
      QApplication app(argc, argv);
  
  
      QQmlApplicationEngine engine;
      ComboBoxModel combo;
  
      QStringList tmp;
      tmp &lt;&lt; "1" &lt;&lt; "2" &lt;&lt; "3" &lt;&lt; "4" &lt;&lt; "5" &lt;&lt; "6" &lt;&lt; "7";
      combo.setComboList(tmp);
  
      QQmlContext *ownContext = engine.rootContext();
      ownContext-&gt;setContextProperty("myModel", QVariant::fromValue(combo.comboList()));
  
      QQmlContext *classContext = engine.rootContext();
      classContext-&gt;setContextProperty("comboModel", &amp;combo);
  
      engine.load(QUrl(QStringLiteral("qrc:/main.qml")));
  
      return app.exec(); }
  </code></pre>
  
  <p><strong>main.qml</strong></p>
  
  <pre><code>import QtQuick 2.3 import QtQuick.Controls 1.2
  
  ApplicationWindow {
      visible: true
      width: 640
      height: 480
      title: qsTr("Hello World")
  
      menuBar: MenuBar {
          Menu {
              title: qsTr("File")
              MenuItem {
                  text: qsTr("&amp;Open")
                  onTriggered: console.log("Open action triggered");
              }
              MenuItem {
                  text: qsTr("Exit")
                  onTriggered: Qt.quit();
              }
          }
      }
  
      Text {
          text: qsTr("Hello World")
          anchors.centerIn: parent
      }
  
      ComboBox {
          id: comboBox1
          x: 258
          y: 54
          model: myModel
          editable: true
          onAccepted: {
               if (editableCombo.find(currentText) === -1) {
                   model.append({text: editText})
                   currentIndex = editableCombo.find(editText)
               }
           }
  
      }
  
      Button {
          id: button1
          x: 280
          y: 139
          text: qsTr("Remove Item")
          onClicked: comboModel.removeElement(comboBox1.currentIndex)
      }
  
      Rectangle{
          x: 281
          y: 316
          width: 80
          height: 20
          color: "white"
  
          TextEdit {
              id: textEdit1
              width: 80
              height: 20
              text: qsTr("Text Edit")
              font.pixelSize: 12
          }
      }
  
      Button {
          id: button2
          x: 280
          y: 388
          text: qsTr("Add Item")
          onClicked: comboModel.addElement(textEdit1.text)
      } }
  </code></pre>