I have a program that uses several tables and I highlight certain cells in the tableView. I have a proxyModel to make this happen. I use almost identical code for several different tables but on one tableView it isn’t showing correctly. The first thing I notice is that the table is shown in reverse order. In other words, the table shows the last item from the database first. All the other tableViews I use, show the data in a first to last order. The other problem is that the correct data is not highlighted. If the table was in the correct order, the correct cell would be highlighted. I think my problem might be with a mapToSource issue but I don’t know what index to use?? (I am a Newbie to Qt and trying to learn) I do not know why the table is shown in reverse order? Also, what events trigger the setData() function?

Here is the code I have for the proxy model:

myproxymodel.h

Qt Code:
  1. #ifndef MYPROXYMODEL_H
  2. #define MYPROXYMODEL_H
  3.  
  4. #include <QSortFilterProxyModel>
  5.  
  6. class MyProxyModel : public QSortFilterProxyModel
  7. {
  8. Q_OBJECT
  9.  
  10. public:
  11. MyProxyModel(QObject* parent);
  12. ~MyProxyModel();
  13. QVariant data ( const QModelIndex & index, int role )const;
  14.  
  15. private:
  16. int m_lowScore;
  17. int m_highScore;
  18. int m_lowReactRow;
  19. int m_fastReactRow;
  20.  
  21. MyProxyModel* proxy;
  22.  
  23. private slots:
  24. void getRows (int, int, int, int);
  25.  
  26. };
  27.  
  28. #endif //MYPROXYMODEL_H
To copy to clipboard, switch view to plain text mode 
myproxymodel.cpp
Qt Code:
  1. #include "myproxymodel.h"
  2.  
  3. MyProxyModel::MyProxyModel (QObject *parent) :
  4. {
  5.  
  6. }
  7. MyProxyModel::~MyProxyModel()
  8. {
  9.  
  10. }
  11.  
  12. QVariant MyProxyModel::data ( const QModelIndex & index, int role ) const
  13. {
  14. QModelIndex sourceIndex = mapToSource(index);
  15.  
  16. if (!sourceIndex.isValid())
  17. return QVariant();
  18. if (m_slowReactRow<0)
  19. {
  20. if ( sourceIndex.row() == m_highScore && sourceIndex.column()== 2 && role == Qt::BackgroundRole )
  21. {
  22. return QVariant( Qt::yellow );
  23. }
  24. else if ( sourceIndex.row() == m_lowScore && sourceIndex.column()== 2 && role == Qt::BackgroundRole )
  25. {
  26. return QVariant( Qt::red );
  27. }
  28.  
  29. else
  30. {
  31. return QSortFilterProxyModel::data( index, role );
  32. }
  33.  
  34. }
  35. else
  36. {
  37. if ( sourceIndex.row() == m_highScore && sourceIndex.column()== 2 && role == Qt::BackgroundRole )
  38. {
  39. return QVariant( Qt::yellow );
  40. }
  41. else if ( sourceIndex.row() == m_lowScore && sourceIndex.column()== 2 && role == Qt::BackgroundRole )
  42. {
  43. return QVariant( Qt::red );
  44. }
  45. else if (sourceIndex.row()== m_fastReactRow && sourceIndex.column()==3 && role == Qt::BackgroundRole)
  46. {
  47. return QVariant( Qt::yellow );
  48. }
  49. else if ( sourceIndex.row() == m_slowReactRow && sourceIndex.column()== 3 && role == Qt::BackgroundRole )
  50. {
  51. return QVariant( Qt::red );
  52. }
  53. else
  54. {
  55. return QSortFilterProxyModel::data( index, role );
  56. }
  57. }
  58. }
  59. void MyProxyModel::getRows( int rowLow,int rowHigh, int rowSlowReact, int rowFastReact)
  60. {
  61. m_highScore=rowHigh;
  62. m_lowScore=rowLow;
  63. m_slowReactRow= rowSlowReact;
  64. m_fastReactRow= rowFastReact;
  65. }
To copy to clipboard, switch view to plain text mode 
This is the table creation code for one of the tableViews that show correctly:
testclass.cpp
Qt Code:
  1. void TestClass::createTimerTable() // this is called in the constructor of the form
  2. {
  3. testModel= new QSqlRelationalTableModel(this);
  4. testModel->setEditStrategy(QSqlTableModel::OnRowChange);
  5. testModel->setTable("testtable");
  6. testModel->setRelation(2,QSqlRelation("student","id", "LName"));
  7. testModel->setRelation(3,QSqlRelation("testNum", "id","Test"));
  8. testModel->setHeaderData(1,Qt::Horizontal,"Section");
  9. testModel->select();
  10.  
  11. proxy = new MyProxyModel (this);
  12. proxy->setSourceModel(testModel);
  13. ui->tableView->setModel(proxy);
  14.  
  15. connect(this,SIGNAL(sendRows(int, int, int, int)),
  16. proxy, SLOT(getRows(int, int, int, int)));
  17.  
  18. //set up tableview
  19. ui->tableView->setItemDelegate(new QSqlRelationalDelegate(this));
  20. ui->tableView->setSelectionMode(QAbstractItemView::SingleSelection);
  21. ui->tableView->setSelectionBehavior(QAbstractItemView::SelectRows);
  22. ui->tableView->setColumnHidden(0,true); // id
  23. ui->tableView->resizeColumnsToContents();
  24. ui->tableView->horizontalHeader()->setStretchLastSection(true);
  25. }
  26. void TestClass::setTable(QString desc) //This is called in the constructor after createTimerTable
  27. {
  28. mTableString=desc;
  29. testModel->setFilter(QString(desc));
  30. testModel->select();
  31. updateTimeStats();
  32. }
  33. void TestClass::updateTimeStats()
  34. {
  35. float totScore=0;
  36. float totReactTime=0;
  37. float lowScore=FLT_MAX;
  38. int rowLowScore=0;
  39. int rowHighScore =0;
  40. int rowSlowReact =0;
  41. int rowFastReact =0;
  42. float slowReactTime =FLT_MIN;
  43. float highScore=FLT_MIN;
  44. float fastReactTime=FLT_MAX;
  45. float currentScore=0;
  46. float currentReactTime =0;
  47. QString string;
  48. int totRows=proxy->rowCount();
  49. if (totRows >0)
  50. {
  51. for (int r=0; r<totRows; r++)
  52. {
  53. currentScore=proxy->index(r,2).data(Qt::DisplayRole).toFloat();
  54. currentReactTime=proxy->index(r,3).data(Qt::DisplayRole).toFloat();
  55.  
  56. if (currentScore < lowScore)
  57. {
  58. lowScore=currentScore;
  59. rowLowScore=r;
  60. }
  61.  
  62. if (currentScore > highScore)
  63. {
  64. highScore=currentScore;
  65. rowHighScore =r;
  66. }
  67.  
  68. if (currentReactTime > slowReactTime)
  69. {
  70. slowReactTime=currentReactTime;
  71. rowSlowReact =r;
  72. }
  73.  
  74. if (currentReactTime<fastReactTime)
  75. {
  76. fastReactTime=currentReactTime;
  77. rowFastReact =r;
  78. }
  79.  
  80. totTime+=currentTime;
  81. totReactTime+=currentReactTime;
  82.  
  83. }
  84. startModel->select();
  85. }
To copy to clipboard, switch view to plain text mode 
Here is the code for the tableView that is in reverse order and not highlighted correctly:
dlgprint.cpp
Qt Code:
  1. void DlgPrint::createReportTable(QStringList stringList) //strinLlist is used elsewhere
  2. {
  3.  
  4. printModel= new QSqlRelationalTableModel (this);
  5. printModel-> setEditStrategy(QSqlTableModel::OnManualSubmit);
  6. printModel-> setTable (mTableName); //string identified through assessor function
  7. printModel-> setRelation (2, QSqlRelation("student", "id", "LName"));
  8. printModel-> setRelation (3, QSqlRelation("testNum", "id", "Test"));
  9. printModel->setFilter(mFilterString);
  10. printModel->select();
  11.  
  12. proxy = new MyProxyModel(this);
  13. proxy->setSourceModel(printModel);
  14. ui->printView->setModel(proxy);
  15.  
  16. connect(this,SIGNAL(sendRows(int, int, int, int)),
  17. proxy, SLOT(getRows(int, int, int, int)));
  18.  
  19. ui->printView->setItemDelegate(new QSqlRelationalDelegate(this));
  20. ui->printView->setSelectionMode(QAbstractItemView::SingleSelection);
  21. ui->printView->setSelectionBehavior(QAbstractItemView::SelectRows);
  22. ui->printView->setColumnHidden(0,true);//id
  23. ui->printView->resizeColumnsToContents();
  24. printModel->setHeaderData (2, Qt::Horizontal, "Score");
  25. ui->printView->setSortingEnabled(true);
  26. ui->printView->resizeColumnsToContents () ;
  27. ui->printView->horizontalHeader()->setStretchLastSection(true);
  28. highlightCells();
  29. printModel->select();
  30. }
  31. void DlgPrint::highlightCells()
  32. {
  33. float highScore= FLT_MIN;
  34. float lowScore= FLT_MAX;
  35. float fastReactionTime=FLT_MAX;
  36. float slowReactionTime=FLT_MIN;
  37. int rowHigh=0;
  38. int rowLow=0;
  39. int rowFastReact=0;
  40. int rowSlowReact=0;
  41. float totalTime =0;
  42. float totalReactTime=0;
  43. float averageScore =0;
  44. float averageReact=0;
  45. int totalRows = 0;
  46. float currentScore;
  47. float currentReactTime;
  48. QString string;
  49.  
  50.  
  51. int numRows =proxy->rowCount();
  52.  
  53. for (int r=0; r<numRows; r++ )
  54. {
  55.  
  56. currentscore = proxy->index(r,2).data(Qt::DisplayRole).toFloat();
  57. currentReactTime = proxy->index(r,3).data(Qt::DisplayRole).toFloat();
  58.  
  59. //something here to mapToSource??
  60.  
  61. if (currentScore > highScore)
  62. {
  63. highScore = currentScore;
  64. rowHigh = r;
  65. }
  66. if (currentScore < lowScore)
  67. {
  68. lowScore = currentScore;
  69. rowLow = r;
  70. }
  71. totalRows++;
  72. totalScore += currentScore;
  73.  
  74.  
  75. if (currentReactTime < fastReactionTime)
  76. {
  77. fastReactionTime = currentReactTime;
  78. rowFastReact = r;
  79. }
  80. if (currentReactTime > slowReactionTime)
  81. {
  82. slowReactionTime = currentReactTime;
  83. rowSlowReact = r;
  84. }
  85. totalReactTime += currentReactTime;
  86. }
  87.  
  88.  
  89. }
  90.  
  91. if (totalRows !=0)
  92. {
  93. averageScore=totalScore/totalRows;
  94. averageReact=totalReactTime/totalRows;
  95. }
  96.  
  97. emit sendRows (rowSlow, rowFast,rowSlowReact, rowFastReact);
  98. }
To copy to clipboard, switch view to plain text mode 
I tried to eliminate the code that was irrelevant to the table creation. Hopefully I didn’t forget something. What is wrong here?