Advertisement
Guest User

Untitled

a guest
Apr 16th, 2014
39
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.85 KB | None | 0 0
  1. class DataObject {
  2. public:
  3. DataObject(const QString &firstName,
  4. const QString &lastName):
  5. first(firstName),
  6. last(lastName) {}
  7.  
  8. QString first;
  9. QString last;
  10. };
  11.  
  12. class SimpleListModel : public QAbstractListModel
  13. {
  14. Q_OBJECT
  15.  
  16. enum /*class*/ Roles {
  17. FIRST_NAME = Qt::UserRole,
  18. LAST_NAME
  19. };
  20.  
  21. public:
  22. SimpleListModel(QObject *parent=0);
  23. QVariant data(const QModelIndex &index, int role) const;
  24. Q_INVOKABLE int rowCount(const QModelIndex &parent = QModelIndex()) const;
  25. QHash<int, QByteArray> roleNames() const;
  26. void addName(QString firstName, QString lastName);
  27.  
  28. private:
  29. Q_DISABLE_COPY(SimpleListModel);
  30. QList<DataObject*> m_items;
  31. };
  32.  
  33. SimpleListModel::SimpleListModel(QObject *parent) :
  34. QAbstractListModel(parent)
  35. {
  36. DataObject *first = new DataObject(QString("Firstname01"), QString("Lastname01"));
  37. DataObject *second = new DataObject(QString("Firstname02"), QString("Lastname02"));
  38. DataObject *third = new DataObject(QString("Firstname03"), QString("Lastname03"));
  39.  
  40. m_items.append(first);
  41. m_items.append(second);
  42. m_items.append(third);
  43. }
  44.  
  45. QHash<int, QByteArray> SimpleListModel::roleNames() const
  46. {
  47. QHash<int, QByteArray> roles;
  48.  
  49. roles[/*Roles::*/FIRST_NAME] = "firstName";
  50. roles[/*Roles::*/LAST_NAME] = "lastName";
  51.  
  52. return roles;
  53. }
  54.  
  55. void SimpleListModel::addName(QString firstName, QString lastName)
  56. {
  57. DataObject *dataObject = new DataObject(firstName, lastName);
  58.  
  59. m_items.append(dataObject);
  60.  
  61. emit dataChanged(this->index(m_items.size()), this->index(m_items.size()));
  62. }
  63.  
  64. int SimpleListModel::rowCount(const QModelIndex &) const
  65. {
  66. return m_items.size();
  67. }
  68.  
  69. QVariant SimpleListModel::data(const QModelIndex &index, int role) const
  70. {
  71. //--- Return Null variant if index is invalid
  72. if(!index.isValid())
  73. return QVariant();
  74.  
  75. //--- Check bounds
  76. if(index.row() > (m_items.size() - 1))
  77. return QVariant();
  78.  
  79. DataObject *dobj = m_items.at(index.row());
  80.  
  81. switch (role)
  82. {
  83. case /*Roles::*/FIRST_NAME:
  84. return QVariant::fromValue(dobj->first);
  85.  
  86. case /*Roles::*/LAST_NAME:
  87. return QVariant::fromValue(dobj->last);
  88.  
  89. default:
  90. return QVariant();
  91. }
  92. }
  93.  
  94. class AppCore : public QObject
  95. {
  96. Q_OBJECT
  97. Q_PROPERTY(SimpleListModel *simpleListModel READ simpleListModel CONSTANT)
  98.  
  99. public:
  100. explicit AppCore(QObject *parent = 0);
  101. SimpleListModel *simpleListModel() const;
  102.  
  103. public slots:
  104. void addName();
  105.  
  106. private:
  107. SimpleListModel *m_SimpleListModel;
  108.  
  109. };
  110.  
  111. AppCore::AppCore(QObject *parent) :
  112. QObject(parent)
  113. {
  114. m_SimpleListModel = new SimpleListModel(this);
  115. }
  116.  
  117. SimpleListModel *AppCore::simpleListModel() const
  118. {
  119. return m_SimpleListModel;
  120. }
  121.  
  122. void AppCore::addName()
  123. {
  124. m_SimpleListModel->addName("FirstnameNEW", "LastnameNEW");
  125. }
  126.  
  127. int main(int argc, char *argv[])
  128. {
  129. QGuiApplication a(argc, argv);
  130.  
  131. QQuickView *view = new QQuickView();
  132. AppCore *appCore = new AppCore();
  133.  
  134. qRegisterMetaType<SimpleListModel *>("SimpleListModel");
  135.  
  136. view->engine()->rootContext()->setContextProperty("appCore", appCore);
  137. view->setSource(QUrl::fromLocalFile("main.qml"));
  138. view->show();
  139.  
  140. return a.exec();
  141. }
  142.  
  143. // ...
  144. ListView {
  145. id: myListView
  146. anchors.fill: parent
  147. delegate: myDelegate
  148. model: appCore.simpleListModel
  149. }
  150.  
  151. MouseArea {
  152. anchors.fill: parent
  153. onClicked: {
  154. appCore.addName()
  155. console.log('rowCount: ' + appCore.simpleListModel.rowCount())
  156. }
  157. }
  158. //...
  159.  
  160. void SimpleListModel::addName(QString firstName, QString lastName)
  161. {
  162. DataObject *dataObject = new DataObject(firstName, lastName);
  163.  
  164. beginInsertRows(ModelIndex(),m_items.size(),m_items.size());
  165. m_items.append(dataObject);
  166. endInsertRows();
  167.  
  168. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement