Results 1 to 7 of 7

Thread: TableView/Proxy problem

  1. #1
    Join Date
    Aug 2010
    Posts
    107
    Thanks
    5
    Qt products
    Qt4
    Platforms
    Windows

    Angry TableView/Proxy problem

    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?

  2. #2
    Join Date
    Feb 2010
    Posts
    18
    Thanked 4 Times in 4 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: TableView/Proxy problem

    Quote Originally Posted by poporacer View Post
    Qt Code:
    1. void DlgPrint::createReportTable(QStringList stringList) //strinLlist is used elsewhere
    2. {
    3. printModel= new QSqlRelationalTableModel (this);
    4. printModel-> setEditStrategy(QSqlTableModel::OnManualSubmit);
    5. printModel-> setTable (mTableName); //string identified through assessor function
    6. printModel-> setRelation (2, QSqlRelation("student", "id", "LName"));
    7. printModel-> setRelation (3, QSqlRelation("testNum", "id", "Test"));
    8. printModel->setFilter(mFilterString);
    9. printModel->select();
    10.  
    11. proxy = new MyProxyModel(this);
    12. proxy->setSourceModel(printModel);
    13. ui->printView->setModel(proxy);
    14.  
    15. connect(this,SIGNAL(sendRows(int, int, int, int)),
    16. proxy, SLOT(getRows(int, int, int, int)));
    17.  
    18. ui->printView->setItemDelegate(new QSqlRelationalDelegate(this));
    19. ui->printView->setSelectionMode(QAbstractItemView::SingleSelection);
    20. ui->printView->setSelectionBehavior(QAbstractItemView::SelectRows);
    21. ui->printView->setColumnHidden(0,true);//id
    22. ui->printView->resizeColumnsToContents();
    23. printModel->setHeaderData (2, Qt::Horizontal, "Score");
    24. ui->printView->setSortingEnabled(true);
    25. ui->printView->resizeColumnsToContents () ;
    26. ui->printView->horizontalHeader()->setStretchLastSection(true);
    27. highlightCells();
    28. printModel->select(); // Why is this?
    29. }
    To copy to clipboard, switch view to plain text mode 
    Why do you select again?

  3. #3
    Join Date
    Aug 2010
    Posts
    107
    Thanks
    5
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: TableView/Proxy problem

    Sorry, that was a typo....but that would not have caused the problem. Any ideas?
    Last edited by poporacer; 15th August 2011 at 15:00. Reason: spelling corrections

  4. #4
    Join Date
    Feb 2008
    Posts
    491
    Thanks
    12
    Thanked 142 Times in 135 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11

    Default Re: TableView/Proxy problem

    From the docs:
    Note:. Setting the property to true with setSortingEnabled() immediately triggers a call to sortByColumn() with the current sort section and order.
    You sort the proxyModel before you highlight the cells. Try the reverse:
    Qt Code:
    1. highlightCells();
    2. ui->printView->setSortingEnabled(true);
    3. ui->printView->resizeColumnsToContents () ;
    4. ui->printView->horizontalHeader()->setStretchLastSection(true);
    5. //highlightCells();
    To copy to clipboard, switch view to plain text mode 
    HTH

  5. #5
    Join Date
    Aug 2010
    Posts
    107
    Thanks
    5
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: TableView/Proxy problem

    That worked great!!!....but why is the tableView in the reverse order? There is no field to sort on when the tableView is created. When the tableView is first created, I want the data to be in the order it is in the source and then let the user sort the data how he pleases.

  6. #6
    Join Date
    Feb 2008
    Posts
    491
    Thanks
    12
    Thanked 142 Times in 135 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11

    Default Re: TableView/Proxy problem

    The following will list the sort section(column) and the sort order for your view:
    Qt Code:
    1. qDebug() << printView->horizontalHeader()->sortIndicatorSection();
    2. qDebug() << printView->horizontalHeader()->QHeaderView::sortIndicatorOrder();
    To copy to clipboard, switch view to plain text mode 
    I tried it on a view and the default sort order seems to be Qt:: DescendingOrder (1). You can set the sort order and sort section before you calling setSortingEnabled() like so:
    Qt Code:
    1. printView->horizontalHeader()->setSortIndicator(0, Qt::AscendingOrder);
    2. printView->setSortingEnabled(true);
    To copy to clipboard, switch view to plain text mode 
    Change the "0" if your db is ordered differently.
    Last edited by norobro; 16th August 2011 at 03:29. Reason: Typo

  7. #7
    Join Date
    Aug 2010
    Posts
    107
    Thanks
    5
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: TableView/Proxy problem

    Thanks.....That fixed it!!!! Now to my next issue....but I will post a new thread for that. I really appreciate the help!!!!

Similar Threads

  1. Webkit + Proxy problem
    By giusepped in forum Qt Programming
    Replies: 1
    Last Post: 21st May 2010, 08:01
  2. QTcpSocket proxy authentication problem
    By arbi in forum Qt Programming
    Replies: 3
    Last Post: 14th September 2009, 17:43
  3. QHttp proxy problem
    By andre.rigon in forum Qt Programming
    Replies: 2
    Last Post: 28th September 2008, 22:19
  4. Proxy problem
    By MrShahi in forum Qt Programming
    Replies: 1
    Last Post: 23rd June 2008, 17:11
  5. QTcpSocket - Proxy problem
    By vishesh in forum Qt Programming
    Replies: 1
    Last Post: 27th September 2007, 00:48

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.