2012-05-08 6 views
1

QTestでQTableViewのセルのエディションをシミュレートしたいと思います。QTestでQTableViewをテストする方法は?

私は別のアプローチを試みたが、している任意の成功なし:私も

QTest::mouseDClick(qtableview) 
QTest::KeyClicks(qtableview,“Hello Word”); 

あなたの助けのおかげ任意の成功なしで3 aproachesに追加した

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 

+1

これが唯一の問題であるかどうかはわかりませんが、 "Hello Word"と入力して "Hello World"と比較すると必ず失敗します。 –

答えて

0

ウィジェットが表示された後にイベントを処理する必要があると思います。 QListViewは隠されていると、おそらく何もしません。 qWaitForWindowShown(qtableview)がこのトリックを行うかどうかを確認してください。それがうまくいかない場合は、QCoreApplication :: processEvents()を呼び出してイベントループを直接実行してください。とにかくqWaitForWindowShownを見て、おそらくイベントループをスピンします。

関連する問題