This is all Qt 4.8:

There is a custom model, FileCopyItemModel, that implements the QAbstractItemModel to hold a list of files that need to be copied. The list of files and their properties are stored in another class (ImageCopyDTO) so QList<ImageCopyDTO> is the actual collection of data. When a QTableView's model is set to an instance of FileCopyItemModel, the columns get their names from headerData(), but no data is ever displayed. The QList<ImageCopyDTO> has elements, but the FileCopyItemModel::data() never gets called. Any idea why?

FileCopyItemModel.h
Qt Code:
  1. typedef QList<ImageCopyDTO> ImageCopyDTOList;
  2.  
  3. class FileCopyItemModel: public QAbstractItemModel
  4. {
  5. Q_OBJECT
  6.  
  7. public:
  8.  
  9. int relativePathNo() const { return 0; }
  10. int noCopyNo() const { return 1; }
  11. int imageName2CopyNo() const { return 2; }
  12. int filesCopyCntNo() const { return 3; }
  13. int qtyNo() const { return 4; }
  14. int productNameNo() const { return 5; }
  15. int notesNo() const { return 6; }
  16.  
  17. explicit FileCopyItemModel(QObject *parent = 0);
  18. ~FileCopyItemModel();
  19.  
  20. virtual QModelIndex index(int row, int column, const QModelIndex &parent = QModelIndex()) const ;
  21. virtual QModelIndex parent(const QModelIndex &child) const;
  22. virtual int rowCount(const QModelIndex &parent = QModelIndex()) const;
  23. virtual int columnCount(const QModelIndex &parent = QModelIndex()) const;
  24. virtual QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const;
  25. bool setData( const QModelIndex & index, const QVariant & value, int role = Qt::EditRole );
  26.  
  27. void clear() { _data.clear(); }
  28.  
  29. QVariant headerData(int section, Qt::Orientation orientation, int role = Qt::DisplayRole) const override;
  30. Qt::ItemFlags flags(const QModelIndex &index) const override;
  31.  
  32. // classic
  33. void append(const ImageCopyDTO& element) { _data.append(element); }
  34. ImageCopyDTOList getElementsFromData() const { return _data; }
  35.  
  36. private:
  37. ImageCopyDTOList _data;
  38. };
To copy to clipboard, switch view to plain text mode 
Qt Code:
  1. FileCopyItemModel::FileCopyItemModel(QObject *parent)
  2. {
  3. }
  4.  
  5. FileCopyItemModel::~FileCopyItemModel(void)
  6. {
  7. }
  8.  
  9. QModelIndex FileCopyItemModel::index(int row, int column, const QModelIndex &parent /*= QModelIndex()*/) const
  10. {
  11. return QModelIndex();
  12. }
  13.  
  14. QModelIndex FileCopyItemModel::parent(const QModelIndex &child) const
  15. {
  16. return QModelIndex();
  17. }
  18.  
  19. int FileCopyItemModel::rowCount(const QModelIndex &parent /*= QModelIndex()*/) const
  20. {
  21. return _data.count();
  22. }
  23.  
  24. int FileCopyItemModel::columnCount(const QModelIndex &parent /*= QModelIndex()*/) const
  25. {
  26. return 7;
  27. }
  28.  
  29. bool FileCopyItemModel::setData( const QModelIndex & index, const QVariant & value, int role /*= Qt::EditRole*/ )
  30. {
  31. bool result = false;
  32.  
  33. int row = index.row();
  34. int column = index.column();
  35.  
  36. if (!index.isValid() || row >= rowCount() || column >= columnCount())
  37. {
  38. return result;
  39. }
  40.  
  41. if(role == Qt::EditRole)
  42. {
  43. switch (column) {
  44. case 1:
  45. _data[row].setNoCopy(value.toBool());
  46. result = true;
  47. break;
  48. case 2:
  49. _data[row].setImageName2Copy(value.toString());
  50. result = true;
  51. break;
  52. case 3:
  53. _data[row].setFilesCopyCnt(value.toInt());
  54. result = true;
  55. break;
  56. default:
  57. break;
  58. } // end switch column
  59. }
  60.  
  61. return result;
  62. }
  63.  
  64. QVariant FileCopyItemModel::data(const QModelIndex &index, int role) const
  65. {
  66. QVariant result;
  67.  
  68. int row = index.row();
  69. int column = index.column();
  70.  
  71. if (!index.isValid() || row >= rowCount() || column >= columnCount())
  72. {
  73. return result;
  74. }
  75.  
  76. switch (role)
  77. {
  78. case Qt::DisplayRole:
  79. // checking row and column to get data
  80. switch (column) {
  81. case 0:
  82. result = _data.at(row).relativePath();
  83. break;
  84. case 1:
  85. result = _data.at(row).noCopy();
  86. break;
  87. case 2:
  88. result = _data.at(row).imageName2Copy();
  89. break;
  90. case 3:
  91. result = _data.at(row).filesCopyCnt();
  92. break;
  93. case 4:
  94. result = _data.at(row).qty();
  95. break;
  96. case 5:
  97. result = _data.at(row).productName();
  98. break;
  99. case 6:
  100. result = _data.at(row).notes();
  101. break;
  102. default:
  103. break;
  104. } // end switch column
  105. break;
  106. default:
  107. break;
  108. }
  109.  
  110. return result;
  111. }
  112.  
  113. QVariant FileCopyItemModel::headerData(int section, Qt::Orientation orientation, int role /*= Qt::DisplayRole*/) const
  114. {
  115. QVariant result = QVariant();
  116.  
  117. if (Qt::DisplayRole == role && Qt::Horizontal == orientation)
  118. {
  119. switch (section)
  120. {
  121. case 0:
  122. result = "Copy";
  123. break;
  124. case 1:
  125. result = "Image Gallery";
  126. break;
  127. case 2:
  128. result = "Image";
  129. break;
  130. case 3:
  131. result = "File Count";
  132. break;
  133. case 4:
  134. result = "Item Qty";
  135. break;
  136. case 5:
  137. result = "Product";
  138. break;
  139. case 6:
  140. result = "Notes";
  141. break;
  142. default:
  143. break;
  144. }
  145. }
  146.  
  147. return result;
  148. }
  149.  
  150. Qt::ItemFlags FileCopyItemModel::flags(const QModelIndex &index) const
  151. {
  152. Qt::ItemFlags result = Qt::ItemIsEditable | QAbstractItemModel::flags(index);
  153. return result;
  154. }
To copy to clipboard, switch view to plain text mode 

Right now a pointer to the instance is being passed into the dialog with the QTableView. In the constructor this is being executed right now:

Qt Code:
  1. ui.fileCopyTableView->setModel(_model);
  2. ui.fileCopyTableView->show();
To copy to clipboard, switch view to plain text mode