Advertisement
Guest User

Untitled

a guest
Apr 24th, 2014
43
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.40 KB | None | 0 0
  1. #include <QtGui/QGuiApplication>
  2. #include <QQmlContext>
  3. #include <QQuickView>
  4. #include <QDebug>
  5. #include "qtquick2applicationviewer.h"
  6.  
  7. #include "QAbstractListModel"
  8.  
  9. class Model : public QAbstractListModel
  10. {
  11. Q_OBJECT
  12. public:
  13. Model() {}
  14.  
  15. int rowCount(const QModelIndex &parent) const
  16. {
  17. return mList.size();
  18. }
  19.  
  20. QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const{
  21. if (index.row() < 0 || index.row() >= mList.size()) {
  22. return QVariant();
  23. }
  24. return mList.at(index.row());
  25. }
  26.  
  27. Q_INVOKABLE QVariant get(int index) {
  28. return data(createIndex(index, 0));
  29. }
  30.  
  31. Q_INVOKABLE void append(QVariant element) {
  32. beginInsertRows(QModelIndex(), mList.size() + 1, mList.size() + 1);
  33. mList.append(element.toMap().value("name").toString());
  34. endInsertRows();
  35. }
  36. private:
  37. QList<QString> mList;
  38. };
  39.  
  40. int main(int argc, char *argv[])
  41. {
  42. QGuiApplication app(argc, argv);
  43.  
  44. QtQuick2ApplicationViewer viewer;
  45. Model model;
  46. viewer.rootContext()->setContextProperty("model", &model);
  47. viewer.setMainQmlFile(QStringLiteral("qml/quick/main.qml"));
  48. viewer.showExpanded();
  49.  
  50. return app.exec();
  51. }
  52.  
  53. #include "main.moc"
  54.  
  55. import QtQuick 2.2
  56.  
  57. Item {
  58. width: 360
  59. height: 360
  60.  
  61. Component.onCompleted: {
  62. model.append({name: "blah"});
  63. console.assert(model.get(0) === "blah");
  64. }
  65. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement