There are several things to consider when trying to edit in a QTableView through simulated events:
A QTableView does not display its cells directly, it does that using its viewport(). Likewise, the double click event must be sent to the viewport instead of the table view itself.
Now when you do
QTest::mouseDClick( m_pTableView->viewport(), Qt::LeftButton,
NULL, QPoint( xPos, yPos ) );
the cell will be selected, but not in edit mode (unlike a human-initiated double click which instantly puts the cell into edit mode even if the table view did not have focus before). If you add a single click on the same location before the double click, however, it will work!
If you then sent the [End] keypress to the viewport, the cursor would not jump to the end of the table cell content, but instead the last cell in the current row would be selected.
In order to change the table cell content, you must send the event to the current editor widget instead. The easiest way to do that is the usage of QWidget::focusWidget()
QTest::keyClick( m_pTableView->viewport()->focusWidget(), Qt::Key_End );
Note that using it like that can be unsafe though as focusWidget() can return NULL.
With that knowledge, the test case can be programmed as follows:
// Note: The table view must be visible at this point
// Retrieve X/Y coordinates of the cell in the third column and the fourth row
int xPos = m_pTableView->columnViewportPosition( 2 ) + 5;
int yPos = m_pTableView->rowViewportPosition( 3 ) + 10;
// Retrieve the viewport of the table view
QWidget* pViewport = m_pTableView->viewport();
// Double click the table cell to set it into editor mode
// Note: A simple double click did not work, Click->Double Click works, however
QTest::mouseClick ( pViewport, Qt::LeftButton, NULL, QPoint( xPos, yPos ) );
QTest::mouseDClick( pViewport, Qt::LeftButton, NULL, QPoint( xPos, yPos ) );
// Simulate [End] keypress
QTest::keyClick( pViewport->focusWidget(), Qt::Key_End );
// Simulate [5] keypress
QTest::keyClick( pViewport->focusWidget(), Qt::Key_5 );
(Note: if you want to verify this, you can add QTest::qWait( 1000 ) commands after each event)
If you are using the _data() functions as described here, note that you cannot retrieve the focusWidget() at the time of data creation.
I solved this issue by creating a custom interface ITestAction
with only a pure virtual "execute()" function. I then added subclasses with a similar constructor as the QTest::mouseClick(...) etc
functions. These classes simply call the QTest functions but use either the widget itself or its focus widget as a parameter, dependent on an additional boolean flag.
The _data() slot then stores a QList< ITestAction* > for each data row, and the actual test slot iterates over that list and calls execute() on each item before the validation is performed.