Hello I am new to Qt and can't figure out why I can add a column header, but the data never populates...It always sends ::data indexes 0-3 as if it doesn't know there is an additional column, but I see 0-4 columns:
Name | Size | Type | Date | [My File Attribute]

What I am doing wrong? Here is my header and implementation file:

FileModel.h
Qt Code:
  1. #ifndef FILEMODEL_H
  2. #define FILEMODEL_H
  3.  
  4. #include <QtGui>
  5.  
  6. class FileModel : public QDirModel
  7. {
  8. QOBJECT
  9.  
  10. public:
  11.  
  12. FileModel(QWidget *parent = 0)
  13.  
  14. ~FileModel();
  15.  
  16. virtual QModelIndex index(int row, int column, const QModelIndex &parent = QModelIndex()) const;
  17.  
  18. virtual QModelIndex index(const QString &path, int column = 0) const;
  19.  
  20. virtual QModelIndex parent(const QModelIndex &index) const;
  21.  
  22. virtual int rowCount(const QModelIndex &parent = QModelIndex()) const;
  23.  
  24. virtual int columnCount(const QModelIndex &parent = QModelIndex()) const;
  25.  
  26. virtual QVariant data(const QModelIndex &index, int role = Qt:DisplayRole) const;
  27.  
  28. virtual QVariant headerData(int section, Qt::Orientation orientation, int role = Qt::DisplayRole) const;
  29. };
  30. #endif // FILEMODEL_H
To copy to clipboard, switch view to plain text mode 

FileModel.cpp
Qt Code:
  1. #include "FileModel.h"
  2.  
  3. FileModel::FileModel(QWidget *parent) : QDirModel(parent)
  4. {
  5. }
  6.  
  7. FileModel::~FileModel()
  8. {
  9. }
  10.  
  11. QModelIndex FileModel::index(int row, int column, const QModelIndex &parent) const
  12. {
  13. return QDirModel::index(row, column, parent);
  14. }
  15.  
  16. QModelIndex FileModel::index(const QString &path, int column) const
  17. {
  18. return QDirModel::index(path, column);
  19. }
  20.  
  21. QModelIndex FileModel::parent(const QModelIndex &index) const
  22. {
  23. return QDirModel::parent(index);
  24. }
  25.  
  26. int FileModel::rowCount(const QModelIndex &parent) const
  27. {
  28. return QDirModel::rowCount(parent);
  29. }
  30.  
  31. int FileModel::columnCount(const QModelIndex &parent) const
  32. {
  33. if (parent.column() > 0)
  34. {
  35. return 0; // This is a safe return??
  36. }
  37. return QDirModel::columnCount(parent) + 1; // Adding one column to QDirModel
  38. }
  39.  
  40. QVariant FileModel::data(const QModelIndex &index, int role) const
  41. {
  42. if (role == Qt::DisplayRole)
  43. {
  44. if (index.column() == QDirModel::columnCount()) // I never ever see 4 for index.column() count, it's ok for QDirModel::columnCount() to return 4, but this always fails.
  45. return tr("File Attribute");
  46. }
  47. return QDirModel::data(index, role);
  48. }
  49.  
  50.  
  51. QVariant FileModel::headerData(int section, Qt::Orientation orientation, int role) const
  52. {
  53. if (orientation == Qt::Horizontal && role == Qt::DisplayRole)
  54. {
  55. switch (section)
  56. {
  57. case 4:
  58. return tr("File Attribute Header");
  59. default:
  60. return QDirModel::headerData(section, orientation, role);
  61. }
  62. }
  63. return QVariant();
  64. }
To copy to clipboard, switch view to plain text mode