Advertisement
Guest User

exposing qabstractlistmodel

a guest
Apr 28th, 2013
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.43 KB | None | 0 0
  1. // main (...)
  2.  
  3.  
  4. SomeList someList;
  5.  
  6. QScopedPointer<QApplication> app(Sailfish::createApplication(argc, argv));
  7.  
  8. QDeclarativeView view;
  9. view.engine()->rootContext()->setContextProperty("somelistmodel", &someList);
  10. view.setSource(QUrl(QString(DEPLOYMENT_PATH) + "/main.qml"));
  11. view.show();
  12.  
  13. return app->exec();
  14.  
  15.  
  16.  
  17. // somelist.h
  18.  
  19.  
  20. enum
  21. {
  22. TitleRole = Qt::UserRole + 1
  23. };
  24.  
  25.  
  26. class SomeList: public QAbstractListModel
  27. {
  28. public:
  29. SomeList()
  30. {
  31. titles.append("1test");
  32. titles.append("2test");
  33. titles.append("3test");
  34. titles.append("4test");
  35.  
  36. }
  37.  
  38. int rowCount(const QModelIndex &parent) const
  39. {
  40. return titles.count();
  41. }
  42.  
  43. QVariant data(const QModelIndex &index, int role) const
  44. {
  45. switch(role)
  46. {
  47. case TitleRole:
  48. return titles.at(index.row());
  49. default:
  50. return QVariant();
  51.  
  52. }
  53. }
  54.  
  55. const QHash<int, QByteArray> roleNames()
  56. {
  57. QHash<int, QByteArray> roles;
  58. roles.insert(TitleRole, "title");
  59. return roles;
  60. }
  61.  
  62. private:
  63. QList<QString> titles;
  64. };
  65.  
  66.  
  67. // main.qml
  68.  
  69. ApplicationWindow
  70. {
  71. initialPage: Page
  72. {
  73. ListView{
  74. anchors.fill: parent
  75.  
  76. model: somelistmodel
  77. delegate: Text{
  78. text: title
  79. }
  80. }
  81.  
  82. }
  83. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement