Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. How to test QTableView cell editing in a unit test?

How to test QTableView cell editing in a unit test?

Scheduled Pinned Locked Moved Unsolved General and Desktop
2 Posts 2 Posters 369 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • L Offline
    L Offline
    laucarlier
    wrote on last edited by
    #1

    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

    SGaistS 1 Reply Last reply
    0
    • L laucarlier

      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

      SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      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.

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      1

      • Login

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • Users
      • Groups
      • Search
      • Get Qt Extensions
      • Unsolved