【问题标题】:How test QTableView edition with QTest?如何用 QTest 测试 QTableView 版本?
【发布时间】:2012-05-09 06:59:34
【问题描述】:

我想用 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 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”);

感谢您的帮助。

【问题讨论】:

  • 不确定这是否是唯一的问题,但您正在输入“Hello Word”并将其与“Hello World”进行比较,这将始终失败。

标签: c++ unit-testing qt qt4 qtestlib


【解决方案1】:

我认为您需要在显示小部件后处理事件。如果隐藏 QListView 可能不会做任何事情(因为它应该!)。看看 qWaitForWindowShown(qtableview) 是否可以解决问题。如果这不起作用,请通过调用 QCoreApplication::processEvents() 直接运行事件循环。看看 qWaitForWindowShown,它可能会旋转一个事件循环。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-11-24
    • 2017-01-27
    • 1970-01-01
    • 2012-07-15
    • 2014-02-19
    • 1970-01-01
    • 2013-05-08
    • 2012-06-02
    相关资源
    最近更新 更多