Hi everyone,

I'm new to the proxy system and I based my work on an old post on the subject made by Santosh Reddy. It work as expected and the dataChanged signals are emit as supposed, but I can't find a way to add content to my new column. The data function is never called so my code there don't affect the view.

Thank you for looking.

Window code:
Qt Code:
  1. const int ADDRESSPOSITION = 4;
  2. const int NBADDRESSCOLUMN = 2;
  3.  
  4. setWindowTitle(QObject::tr("Liste des clients"));
  5.  
  6. m_pFenCustomer = new Fen_Customer(this);
  7.  
  8. pModel->setTable("customer");
  9. pModel->setRelation(1, QSqlRelation("address", "idAddress", "address"));
  10. pModel->setEditStrategy(QSqlTableModel::OnManualSubmit);
  11. pModel->select();
  12. pModel->setHeaderData(1, Qt::Horizontal, QObject::tr("Adresse"));
  13. pModel->setHeaderData(3, Qt::Horizontal, QObject::tr("Nom du Client"));
  14. pModel->setHeaderData(4, Qt::Horizontal, QObject::tr("Numéro de Téléphone"));
  15. pModel->setHeaderData(5, Qt::Horizontal, QObject::tr("Poste"));
  16. pModel->setHeaderData(6, Qt::Horizontal, QObject::tr("Courriel"));
  17.  
  18. InsertProxyModel *pProxyModel = new InsertProxyModel(ADDRESSPOSITION,NBADDRESSCOLUMN,this);
  19. pProxyModel->setSourceModel(pModel);
  20.  
  21. tableView->setModel(pProxyModel);
  22.  
  23. tableView->hideColumn(1);
  24. tableView->hideColumn(2);
To copy to clipboard, switch view to plain text mode 

InsertProxyModel.h
Qt Code:
  1. #include <QAbstractProxyModel>
  2. #include <QStringList>
  3.  
  4. class InsertProxyModel : public QAbstractProxyModel
  5. {
  6. Q_OBJECT
  7.  
  8. public:
  9. InsertProxyModel(int insertPos, int nbCol, QObject *parent = 0);
  10. virtual ~InsertProxyModel();
  11.  
  12. QModelIndex index(int row, int column, const QModelIndex &parent = QModelIndex()) const;
  13. QModelIndex parent(const QModelIndex & /*child*/) const;
  14. int rowCount(const QModelIndex &parent = QModelIndex()) const;
  15. int columnCount(const QModelIndex &parent = QModelIndex()) const;
  16. QModelIndex mapToSource(const QModelIndex &proxyIndex) const;
  17. QModelIndex mapFromSource(const QModelIndex &sourceIndex) const;
  18. QVariant headerData(int section, Qt::Orientation orientation, int role = Qt::DisplayRole) const;
  19. QVariant data(const QModelIndex &index, int role);
  20. bool setData(const QModelIndex & index, const QVariant & value, int role = Qt::EditRole);
  21. Qt::ItemFlags flags(const QModelIndex &index) const;
  22.  
  23. private:
  24. int m_insertPos;
  25. int m_nbCol;
  26.  
  27. QStringList mData;
  28.  
  29. public slots:
  30. void slotSourceModelChanged(void);
  31. void slotDataChanged(const QModelIndex first, QModelIndex last);
  32.  
  33. };
To copy to clipboard, switch view to plain text mode 

InsertProxyModel.cpp
Qt Code:
  1. #include "insertProxyModel.h"
  2.  
  3. //--------------------------------------------------------------------------
  4.  
  5. InsertProxyModel::InsertProxyModel(int insertPos, int nbCol, QObject *parent) : QAbstractProxyModel(parent), mData()
  6. {
  7. m_insertPos = insertPos;
  8. m_nbCol = nbCol;
  9.  
  10. connect(this, SIGNAL(sourceModelChanged()), this, SLOT(slotSourceModelChanged()));
  11. }
  12.  
  13. //--------------------------------------------------------------------------
  14.  
  15. InsertProxyModel::~InsertProxyModel()
  16. {
  17. }
  18.  
  19. //--------------------------------------------------------------------------
  20.  
  21. QModelIndex InsertProxyModel::index(int row, int column, const QModelIndex & parent) const
  22. {
  23. if(!parent.isValid())
  24. {
  25. res = createIndex(row, column);
  26. }
  27. return res;
  28. }
  29.  
  30. //--------------------------------------------------------------------------
  31.  
  32. QModelIndex InsertProxyModel::parent(const QModelIndex & /*child*/) const
  33. {
  34. return QModelIndex();
  35. }
  36.  
  37. //--------------------------------------------------------------------------
  38.  
  39. int InsertProxyModel::rowCount(const QModelIndex & parent) const
  40. {
  41. int res = 0;
  42. if(!parent.isValid())
  43. {
  44. res = (sourceModel()->rowCount(QModelIndex()));
  45. }
  46. return res;
  47. }
  48.  
  49. //--------------------------------------------------------------------------
  50.  
  51. int InsertProxyModel::columnCount(const QModelIndex & parent) const
  52. {
  53. int res = 0;
  54. if(!parent.isValid())
  55. {
  56. res = sourceModel()->columnCount() + m_nbCol;
  57. }
  58. return res;
  59. }
  60.  
  61. //--------------------------------------------------------------------------
  62.  
  63. QModelIndex InsertProxyModel::mapToSource(const QModelIndex & proxyIndex) const
  64. {
  65. if(proxyIndex.isValid())
  66. {
  67. if(proxyIndex.column() >= m_insertPos && proxyIndex.column() <= (m_insertPos+m_nbCol-1))
  68. {
  69. res = createIndex(proxyIndex.row(), -1, (quintptr)-1);
  70. }
  71. else if(proxyIndex.column() < m_insertPos)
  72. {
  73. res = sourceModel()->index(proxyIndex.row(), proxyIndex.column());
  74. }
  75. else
  76. {
  77. res = sourceModel()->index(proxyIndex.row(), proxyIndex.column()-m_nbCol);
  78. }
  79. }
  80. return res;
  81. }
  82.  
  83. //--------------------------------------------------------------------------
  84.  
  85. QModelIndex InsertProxyModel::mapFromSource(const QModelIndex & sourceIndex) const
  86. {
  87. if(!sourceIndex.isValid())
  88. {
  89. if((sourceIndex.row() > -1) &&
  90. (sourceIndex.column() >= m_insertPos && sourceIndex.column() <= (m_insertPos+m_nbCol-1)) &&
  91. (sourceIndex.internalId() == (quintptr)(sourceIndex.column())))
  92. {
  93. res = index(sourceIndex.row(), sourceIndex.column());
  94. }
  95. }
  96. else
  97. {
  98. res = index(sourceIndex.row(), sourceIndex.column());
  99. }
  100. return res;
  101. }
  102.  
  103. //--------------------------------------------------------------------------
  104.  
  105. QVariant InsertProxyModel::headerData(int section, Qt::Orientation orientation, int role) const
  106. {
  107. QVariant res;
  108. if(orientation == Qt::Horizontal)
  109. {
  110. if(section >= m_insertPos && section <= (m_insertPos+m_nbCol-1))
  111. {
  112. res = QString("Proxy Column");
  113. }
  114. else if(section < m_insertPos)
  115. {
  116. res = sourceModel()->headerData(section, orientation, role);
  117. }
  118. else
  119. {
  120. res = sourceModel()->headerData(section-m_nbCol, orientation, role);
  121. }
  122. }
  123. else
  124. {
  125. res = sourceModel()->headerData(section, orientation, role);
  126. }
  127. return res;
  128. }
  129.  
  130. //--------------------------------------------------------------------------
  131.  
  132. QVariant InsertProxyModel::data(const QModelIndex &index, int role)
  133. {
  134. QVariant res;
  135. if(index.column() >= m_insertPos && index.column() <= (m_insertPos+m_nbCol-1))
  136. {
  137. if((role == Qt::DisplayRole) || (role == Qt::EditRole))
  138. {
  139. if(index.row() < mData.size())
  140. {
  141. //res = mData.at(index.row());
  142. //I try to put the same test text in all the added column here
  143. res = "bleh";
  144. }
  145. }
  146. }
  147. else
  148. {
  149. res = sourceModel()->data(mapToSource(index), role);
  150. }
  151. return res;
  152. }
  153.  
  154. //--------------------------------------------------------------------------
  155.  
  156. bool InsertProxyModel::setData(const QModelIndex & index, const QVariant & value, int role)
  157. {
  158. bool res = false;
  159. if(index.column() >= m_insertPos && index.column() <= (m_insertPos+m_nbCol-1))
  160. {
  161. if((role == Qt::DisplayRole) || (role == Qt::EditRole))
  162. {
  163. while((index.row() + 1) > mData.size())
  164. {
  165. mData.append(QString());
  166. }
  167. mData[index.row()] = value.toString();
  168. return true;
  169. }
  170. }
  171. else
  172. {
  173. res = sourceModel()->setData(mapToSource(index), value, role);
  174. }
  175. return res;
  176. }
  177.  
  178. //--------------------------------------------------------------------------
  179.  
  180. Qt::ItemFlags InsertProxyModel::flags(const QModelIndex &index) const
  181. {
  182. Qt::ItemFlags res;
  183. if(index.column() >= m_insertPos && index.column() <= (m_insertPos+m_nbCol-1))
  184. {
  185. res = Qt::ItemIsSelectable | Qt::ItemIsEnabled;
  186. }
  187. else
  188. {
  189. res = sourceModel()->flags(mapToSource(index));
  190. }
  191. return res;
  192. }
  193.  
  194. //--------------------------------------------------------------------------
  195.  
  196. void InsertProxyModel::slotSourceModelChanged(void)
  197. {
  198. disconnect(this, SLOT(slotDataChanged(QModelIndex,QModelIndex)));
  199. connect(sourceModel(), SIGNAL(dataChanged(QModelIndex,QModelIndex)), this, SLOT(slotDataChanged(QModelIndex,QModelIndex)));
  200. }
  201.  
  202. //--------------------------------------------------------------------------
  203.  
  204. void InsertProxyModel::slotDataChanged(const QModelIndex first, QModelIndex last)
  205. {
  206. emit dataChanged(mapFromSource(first), mapFromSource(last));
  207. }
  208.  
  209. //--------------------------------------------------------------------------
To copy to clipboard, switch view to plain text mode