How to test QTableView cell editing in a unit test?
Unsolved
General and Desktop
-
Hello,
I would like to verify in a unit test that my cell editing is working fine within a QTableView.
I have the following unit test which is not working and I'm wondering why (see below the crash message and the callstack)
void TestItemsManager::checkModifyPrice() { ItemsModel model{}; // This is a QAbstractTableModel ItemsManager itemsManager{nullptr, &model}; // This is a QDialog ClickButtonAndCheckRowAdded(itemsManager, model, 0); auto tableView = itemsManager.findChild<QTableView*>("tableView"); QCOMPARE_NE(tableView, nullptr); QModelIndex index = model.index(0, 1); QPoint pointToClick = tableView->visualRect(index).center(); QTest::mouseDClick(tableView->viewport(), Qt::MouseButton::LeftButton, Qt::NoModifier, pointToClick); QTest::keyClicks(tableView->keyboardGrabber(), "3"); QTest::keyClick(tableView->keyboardGrabber(), Qt::Key_Enter); std::array<QVariant, 10> rowToExpect = { QVariant{""}, QVariant{"3.00 €"}, QVariant{"15 %"}, QVariant{"20 %"}, QVariant{"0.45 €"}, QVariant{"3.45 €"}, QVariant{"0.69 €"}, QVariant{"4.14 €"}, QVariant{"0.87 €"}, QVariant{"5.01 €"} }; for(int idx = 0; idx < rowToExpect.size(); ++idx) { qDebug() << "idx = " << idx; QCOMPARE(model.data(model.index(0, idx)), rowToExpect[idx]); } }
Here is the crash message
QFATAL : TestItemsManager::checkModifyPrice() ASSERT: "widget" in file C:\Qt\6.4.2\msvc2019_64\include\QtTest/qtestkeyboard.h, line 194 FAIL! : TestItemsManager::checkModifyPrice() Received a fatal error. C:\Qt\6.4.2\msvc2019_64\include\QtTest/qtestkeyboard.h(194) : failure location
And the call stack
1 qAbort qglobal.cpp 3358 0x7fffe0e890fa 2 qt_message_fatal qlogging.cpp 1917 0x7fffe0e9df8a 3 QMessageLogger::fatal qlogging.cpp 851 0x7fffe0e9c3e8 4 qt_assert qglobal.cpp 3276 0x7fffe0e872b9 5 QTest::sendKeyEvent qtestkeyboard.h 194 0x7ff7af0662a8 6 QTest::sendKeyEvent qtestkeyboard.h 249 0x7ff7af066cf4 7 QTest::keyEvent qtestkeyboard.h 254 0x7ff7af066d6a 8 QTest::keyClicks qtestkeyboard.h 263 0x7ff7af066e6d 9 TestItemsManager::checkModifyPrice testItemsManager.cpp 161 0x7ff7af0687ca 10 TestItemsManager::qt_static_metacall testItemsManager.moc 237 0x7ff7af067af6 11 QMetaMethod::invoke qmetaobject.cpp 2358 0x7fffe0fb1915 12 QMetaMethod::invoke qmetaobject.h 92 0x7fffe0ebbe52 13 QTest::TestMethods::invokeTestOnData qtestcase.cpp 1124 0x7ffffa3eda51 14 QTest::TestMethods::invokeTest qtestcase.cpp 1397 0x7ffffa3eb8d7 15 QTest::TestMethods::invokeTests qtestcase.cpp 1722 0x7ffffa3ea55d 16 QTest::qRun qtestcase.cpp 2336 0x7ffffa3e3bd6 17 QTest::qExec qtestcase.cpp 2222 0x7ffffa3e3ceb 18 main testItemsManager.cpp 184 0x7ff7af068cf6 19 WinMain qtentrypoint_win.cpp 50 0x7ff7af081a22 20 invoke_main exe_common.inl 107 0x7ff7af07f052 ... <More>
I do not want to directly call the
setData
function of my model because I want to make sure that the user can actually edit the cell by double clicking on the cell.Can anybody tell me what I'm missing here?
Many thanks,
Kind regards,
Laurent
-
Hi and welcome to devnet,
You should check that you actually got an editor.
Did you consider using the edit method ? It would be simpler than trying to click on the table view.