PDA

View Full Version : QTable selections in Qt3.3



Rayven
24th June 2007, 23:06
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.


connect( this, SIGNAL( clicked( int, int, int, const QPoint& ) ), this, SLOT( changeSelection( int, int, int, const QPoint& ) ) );


void Table::changeSelection( int row, int col, int button, const QPoint &mousePos )
{
if( button == Qt::LeftButton )
{
bool selected;

// Check if the current row is supposed to be selected
if( mpTableData->isEventSelected( row ) )
{
selected = false;
}
else
{
selected = true;
}

int selectedValue = mpTableData->getEventData( row );

// Loop through all the data elements checking if there are duplicates event data
for( int event = 0; event < mpTableData->getTotalNumEvents( ); event++ )
{
if( selectedValue == mpTableData->getEventData( row ) )
{
// If the events match, select/deselect both events
mpTableData->selectEvent( event, select ); // Sets the flag in the Data Model
}
}
updateSelections( );
}
}

void Table::updateSelections( )
{
blockSignals( );

clearSelection( );

for( int event = 0; event < mpTableData->getTotalNumEvents( ); event++ )
{
if( !mpTableData->isEventHidden( event )
if( mpTableData->isEventSelected( event ) )
addSelection( QTableSelection( row, 0, row, numCols( ) - 1 ) );

row = row + 1;
}

blockSignals( false );
}


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!! :D

Thanks in advance.

Rayven
7th July 2007, 18:07
After much hair pulling, with the help of ToddAtWSU we were able to figure out a solution: we had to capture the mouse press and release events and perform the selections during the events using contentsMouseDoubleClickEvent(). What threw me was since I am used to Qt4, this was a different event handler than I am used to.