How can you edit a QTableView cell from a QTest unit test?

╄→尐↘猪︶ㄣ 提交于 2020-12-29 06:50:09

问题


I'm writing a unit test for a custom Validator in a QTableView using the QTestLib framework. One of the most basic test cases could be described like this:

Double click the table cell in the third column and the fourth row, and append the number '5' to its content.

It is not sufficient to simply change the value in the model or anything, the test case shall perform it like this:

  1. Double Click the table cell to set it into edit mode
  2. Press the [End] key.
  3. Press the [5] key.

Note: This question has an answer on how to set a table cell into edit mode from code, however the unit test shall try to stick to the possibilities of a human user, i.e. Mouse/Keyboard actions.

I've found out the X/Y position of a cell can be retrieved using QTableView::columnViewportPosition( int ) and QTableView::rowViewportPosition( int ). However, double clicking at the specified location using QTest::mouseDClick(...) neither selects the cell nor sets it into edit mode:

// Retrieve X/Y coordinates of the cell in the third column and the fourth row
int xPos = m_pTableView->columnViewportPosition( 2 );
int yPos = m_pTableView->rowViewportPosition( 3 );

// This does not work
QTest::mouseDClick( m_pTableView, Qt::LeftButton, QPoint( xPos, yPos ) );

How can I implement the test case which I described above, using Mouse/Keyboard actions only?

PS: I am trying this under Windows XP 32 bit and Qt 4.6.1


回答1:


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.



来源:https://stackoverflow.com/questions/12604739/how-can-you-edit-a-qtableview-cell-from-a-qtest-unit-test

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!