Important: Please read the Qt Code of Conduct -

How test QTableView edition with QTest ?

  • Hello,

    I would like to simulate edition of a cell of QTableView with QTest.

    I have tried different approach but without any success:

    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(qtableview->model()->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(qtableview->model()->index(1,1).data(), "Hello World"); // --> FAILED

    I have also added in the three aproaches without any success
    QTest::KeyClicks(qtableview,"Hello Word");

    Thanks for your help.

Log in to reply