2012-05-08 60 views
1

我想用QTest來模擬QTableView的一個單元版本。如何使用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 

我也曾在這三個aproaches添加沒有任何成功

QTest::mouseDClick(qtableview) 
QTest::KeyClicks(qtableview,「Hello Word」); 

感謝您的幫助。

+1

不知道這是否是唯一的問題,但是您輸入「Hello Word」並將其與「Hello World」進行比較,後者將始終失敗。 –

回答

0

我認爲您需要在顯示小部件後處理事件。 QListView可能不會做任何事情,如果它被隱藏(因爲它應該!)。看看qWaitForWindowShown(qtableview)是否能做到這一點。如果這不起作用,則通過調用QCoreApplication :: processEvents()直接運行事件循環。看看qWaitForWindowShown,無論如何它可能會旋轉事件循環。