C++ 如何使用QTest测试QTableView版本?
我想用QTest模拟QTableView单元格的编辑 我尝试过不同的方法,但没有成功:C++ 如何使用QTest测试QTableView版本?,c++,unit-testing,qt,qt4,qtestlib,C++,Unit Testing,Qt,Qt4,Qtestlib,我想用QTest模拟QTableView单元格的编辑 我尝试过不同的方法,但没有成功: qtableview->show(); /* I think that in my unit test I should no need that, could you confirm ? */ QModelIndex modelIndex = qtableview->model()->index(1,1); //I have tested that modelIndex is va
qtableview->show();
/* I think that in my unit test I should no need
that, could you confirm ? */
QModelIndex modelIndex = qtableview->model()->index(1,1);
//I have tested that modelIndex is valid and that I retrieved expected data
/*First try: set the currentIndex on modelIndex
thinking that keyClicks on qtableview will work
on selected element of the tableview*/
qtableview->setCurrentIndex(modelIndex );
QTest::KeyClicks(qtableview,“Hello Word”);
QCOMPARE->index(1,1).data(), “Hello World”); // —> FAILED
/*Second approach
Get the cell widget*/
QWidget * qwidget = qtableview->indexWidget( modelIndex );
//—> No test since the qwidget is NULL… why ?
/*Third approach
Get the cell widget through the delegate*/
QWidget * qwidget =
qtableview->itemDelegate( modelIndex )->createEditor(qtableview,
QStyleOptionViewItem(),
modelIndex );
QTest::KeyClicks(qwidget ,“Hello Word”);
QCOMPARE->index(1,1).data(), “Hello World”); // —> FAILED
我还加入了三个步骤,但没有成功
QTest::mouseDClick(qtableview)
QTest::KeyClicks(qtableview,“Hello Word”);
感谢您的帮助。我认为您需要在显示小部件后处理事件。如果QListView像它应该做的那样被隐藏,它可能不会做任何事情!。看看qWaitForWindowShownqtableview是否能做到这一点。如果这不起作用,请通过调用QCoreApplication::processEvents直接运行事件循环。看看qWaitForWindowShown,它可能会旋转一个事件循环。不确定这是否是唯一的问题,但您正在键入Hello Word并将其与Hello World进行比较,Hello World总是会失败。