I am subclassing QTable in order to have refined control over the column sorting and the selection of the rows. I am using Qt 3.3 for company reasons and do not have control over switching to Qt 4. I have created a pseudo "Variant Table Model" class that stores whether the row is selected or hidden. The TableModel class is sorted whenever a column header is clicked and the table is cleared and repopulated in the new sorted. If the selection is changed by highlighting a row, the table is cleared and redrawn with the new selection, keeping the old rows selected.
Qt Code:
  1. connect( this, SIGNAL( clicked( int, int, int, const QPoint& ) ), this, SLOT( changeSelection( int, int, int, const QPoint& ) ) );
  2.  
  3.  
  4. void Table::changeSelection( int row, int col, int button, const QPoint &mousePos )
  5. {
  6. if( button == Qt::LeftButton )
  7. {
  8. bool selected;
  9.  
  10. // Check if the current row is supposed to be selected
  11. if( mpTableData->isEventSelected( row ) )
  12. {
  13. selected = false;
  14. }
  15. else
  16. {
  17. selected = true;
  18. }
  19.  
  20. int selectedValue = mpTableData->getEventData( row );
  21.  
  22. // Loop through all the data elements checking if there are duplicates event data
  23. for( int event = 0; event < mpTableData->getTotalNumEvents( ); event++ )
  24. {
  25. if( selectedValue == mpTableData->getEventData( row ) )
  26. {
  27. // If the events match, select/deselect both events
  28. mpTableData->selectEvent( event, select ); // Sets the flag in the Data Model
  29. }
  30. }
  31. updateSelections( );
  32. }
  33. }
  34.  
  35. void Table::updateSelections( )
  36. {
  37. blockSignals( );
  38.  
  39. clearSelection( );
  40.  
  41. for( int event = 0; event < mpTableData->getTotalNumEvents( ); event++ )
  42. {
  43. if( !mpTableData->isEventHidden( event )
  44. if( mpTableData->isEventSelected( event ) )
  45. addSelection( QTableSelection( row, 0, row, numCols( ) - 1 ) );
  46.  
  47. row = row + 1;
  48. }
  49.  
  50. blockSignals( false );
  51. }
To copy to clipboard, switch view to plain text mode 

Printing the Model values, produces the correct selected rows at least through the model, but when the code goes through the addSelection( ) the rows are not selected correctly. For example: select row 1 on the table, the model selects event 1 correctly and the table highlights row one. Next select row 4 on the table, the model selects event 4 (in addition to event 1) but the table will only hightlight row 4 not row 1. Next select row 2, events 2, 4, and 1 are selected in the model, but the actual table will only highlight rows 1 and 2 and not 4. The addSelection is called for rows 1, 2 and 4 but only 1 and 2 will actually be highlighted. The rows that are highlighted varies from run to run, the model contents are always correct, but the highlighted rows on the QTable are not. I am at a loss as to why this is happening, but I am hoping that someone here may have a great idea!!

Thanks in advance.