Pastebin launched a little side project called VERYVIRAL.com, check it out ;-) Want more features on Pastebin? Sign Up, it's FREE!
Guest

Untitled

By: a guest on Nov 30th, 2011  |  syntax: C++  |  size: 2.33 KB  |  views: 24  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. //My Model
  2. #include "Model.h"
  3. #include <QDebug>
  4.  
  5.  
  6. Model::Model(QObject *parent) :
  7.     QAbstractItemModel(parent)
  8. {
  9. }
  10.  
  11. QModelIndex Model::index(int r, int c, const QModelIndex&) const
  12. {
  13.     return createIndex(r,c);
  14. }
  15. QModelIndex Model::parent(const QModelIndex&) const
  16. {
  17.     return QModelIndex();
  18. }
  19. int Model::rowCount(const QModelIndex&) const
  20. {
  21. static int a = 0;
  22. qDebug() << "Model::rowCount data_.size() " << a++ << ": " << data_.size();
  23.   return data_.size();
  24. }
  25. int Model::columnCount(const QModelIndex&) const
  26. {
  27.     return 1;
  28. }
  29. QVariant Model::data(const QModelIndex& index, int role) const
  30. {
  31.     switch (role)
  32.     {
  33.      case Qt::DisplayRole:
  34.     {
  35.         static int a = 0;
  36.         if (data_.size() > index.row())
  37.         {
  38.         //qDebug() << "Model::data data_[index.row()] " << a++ << ": " << data_.at(index.row());
  39.  
  40.         return data_.at(index.row());
  41.         }
  42.         else
  43.         {
  44.             return QVariant();
  45.         }
  46.     }
  47.     default:
  48.         return QVariant();
  49.     }
  50. }
  51.  
  52. bool Model::set_data(int data)
  53. {
  54.     beginInsertRows(QModelIndex(),0,data_.size());
  55.     data_.append(data);
  56.     static int a = 0;
  57.     qDebug() << "Model::set_data data_ " << a++ << ":" << data_;
  58.     endInsertRows();
  59.     emit dataChanged(createIndex(0,0),createIndex(data_.size(),0));
  60.     return true;
  61. }
  62.  
  63. //My Proxy
  64.  
  65.  
  66. #include "Proxy.h"
  67. #include <QDebug>
  68. Proxy::Proxy(QAbstractItemModel* source_model,QObject *parent) :
  69.     QAbstractProxyModel(parent)
  70. {
  71.  
  72. }
  73.  
  74.  
  75. QModelIndex Proxy::mapToSource(const QModelIndex & proxyIndex) const
  76. {
  77.  
  78.     return sourceModel()->index(proxyIndex.row(),proxyIndex.column());
  79. }
  80.  
  81. QModelIndex Proxy::mapFromSource(const QModelIndex & sourceIndex) const
  82. {
  83.  
  84.     return sourceModel()->index(sourceIndex.row(),sourceIndex.column());
  85. }
  86.  
  87. QModelIndex Proxy::index(int row, int column,
  88.                           const QModelIndex &parent) const
  89. {
  90.     return createIndex(row,column);
  91. }
  92.  
  93. QModelIndex Proxy::parent(const QModelIndex &child) const
  94. {
  95.     return QModelIndex();
  96. }
  97.  
  98. int Proxy::rowCount(const QModelIndex &parent) const
  99. {
  100.     static int i = 0;
  101.     qDebug() << "Proxy row count " << i++ << ": " << sourceModel()->rowCount();
  102.     return sourceModel()->rowCount();
  103. }
  104. int Proxy::columnCount(const QModelIndex &parent) const
  105. {
  106.     return sourceModel()->columnCount();
  107. }
  108.  
  109.  
clone this paste RAW Paste Data