I am using QTableview + QAbastractTableModel subclass to display order trading data. It works great. But when I use QSortFilterProxyModel for table sorting/filtering, the application crashes if I sort the rows and then add new data row. Sometimes, even a click in the table would result in a crash. I have not changed any codes in the Model . The following are the codes related. Please kindly help.

VIEW:
Qt Code:
  1. pTableView = new QTableView();
  2. pModel = new TradeManagerModel(0);
  3. proxyModel = new QSortFilterProxyModel(this);
  4. proxyModel->setSourceModel(pModel);
  5. proxyModel->setDynamicSortFilter(true);
  6. pTableView->setModel(proxyModel);
  7. pTableView->setSortingEnabled(true);
To copy to clipboard, switch view to plain text mode 

MODEL: The data is stored in QMap<QString, QStringList> mGridData.
Qt Code:
  1. int TradeManagerModel::rowCount(const QModelIndex & /*parent*/) const
  2. {
  3. return mGridData.size();
  4. }
  5.  
  6. int TradeManagerModel::columnCount(const QModelIndex & /*parent*/) const
  7. {
  8. return 10;
  9. }
  10.  
  11. QVariant TradeManagerModel::data(const QModelIndex &index, int role) const
  12. {
  13. if (role == Qt::DisplayRole)
  14. {
  15. return mGridData[mGridData.keys().at(index.row())].at(index.column());
  16. }
  17. return QVariant();
  18. }
  19.  
  20. QVariant TradeManagerModel::headerData(int section, Qt::Orientation orientation, int role) const
  21. {
  22.  
  23. if (role == Qt::DisplayRole)
  24. {
  25. if (orientation == Qt::Horizontal) {
  26. switch (section)
  27. {
  28. case 0:
  29. return QString("TIME");
  30. case 1:
  31. return QString("SYMBOL");
  32. case 2:
  33. return QString("B/S");
  34. case 3:
  35. return QString("QTY");
  36. case 4:
  37. return QString("PRICE");
  38. case 5:
  39. return QString("COMM");
  40. case 6:
  41. return QString("STRATEGYID");
  42. case 7:
  43. return QString("EXECID");
  44. case 8:
  45. return QString("ORDERID");
  46. case 9:
  47. return QString("");
  48. }
  49. }
  50. }
  51. return QVariant();
  52. }
  53.  
  54. void TradeManagerModel::onTradeItemUpdated(const TradeItem &tradeItem)
  55. {
  56. QString execId = tradeItem.execId;
  57.  
  58. QStringList tmpStrList;
  59. tmpStrList << tradeItem.tradeTimeStamp
  60. << tradeItem.symbol
  61. << tradeItem.action
  62. << QString::number(tradeItem.fillQty)
  63. << QString::number(tradeItem.fillPrice)
  64. << QString::number(tradeItem.commission)
  65. << tradeItem.orderRef
  66. << execId
  67. << tradeItem.orderId
  68. <<"";
  69.  
  70. if(!mGridData.contains(execId))
  71. {
  72. mGridData[execId] = tmpStrList;
  73. emit layoutChanged();
  74. }
  75.  
  76. }
To copy to clipboard, switch view to plain text mode