Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8593767
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T00:03:51+00:00 2026-06-12T00:03:51+00:00

I’m writing a unit test for a custom Validator in a QTableView using the

  • 0

I’m writing a unit test for a custom Validator in a QTableView using the QTestLib framework. One of the most basic test cases could be described like this:

Double click the table cell in the third column and the fourth row, and append the number ‘5’ to its content.

It is not sufficient to simply change the value in the model or anything, the test case shall perform it like this:

  1. Double Click the table cell to set it into edit mode
  2. Press the [End] key.
  3. Press the [5] key.

Note: This question has an answer on how to set a table cell into edit mode from code, however the unit test shall try to stick to the possibilities of a human user, i.e. Mouse/Keyboard actions.

I’ve found out the X/Y position of a cell can be retrieved using QTableView::columnViewportPosition( int ) and QTableView::rowViewportPosition( int ).
However, double clicking at the specified location using QTest::mouseDClick(…) neither selects the cell nor sets it into edit mode:

// Retrieve X/Y coordinates of the cell in the third column and the fourth row
int xPos = m_pTableView->columnViewportPosition( 2 );
int yPos = m_pTableView->rowViewportPosition( 3 );

// This does not work
QTest::mouseDClick( m_pTableView, Qt::LeftButton, QPoint( xPos, yPos ) );

How can I implement the test case which I described above, using Mouse/Keyboard actions only?

PS: I am trying this under Windows XP 32 bit and Qt 4.6.1

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T00:03:52+00:00Added an answer on June 12, 2026 at 12:03 am

    There are several things to consider when trying to edit in a QTableView through simulated events:

    A QTableView does not display its cells directly, it does that using its viewport(). Likewise, the double click event must be sent to the viewport instead of the table view itself.

    Now when you do

    QTest::mouseDClick( m_pTableView->viewport(), Qt::LeftButton, 
                        NULL, QPoint( xPos, yPos ) );
    

    the cell will be selected, but not in edit mode (unlike a human-initiated double click which instantly puts the cell into edit mode even if the table view did not have focus before). If you add a single click on the same location before the double click, however, it will work!

    If you then sent the [End] keypress to the viewport, the cursor would not jump to the end of the table cell content, but instead the last cell in the current row would be selected.
    In order to change the table cell content, you must send the event to the current editor widget instead. The easiest way to do that is the usage of QWidget::focusWidget()

    QTest::keyClick( m_pTableView->viewport()->focusWidget(), Qt::Key_End );
    

    Note that using it like that can be unsafe though as focusWidget() can return NULL.


    With that knowledge, the test case can be programmed as follows:

    // Note: The table view must be visible at this point
    
    // Retrieve X/Y coordinates of the cell in the third column and the fourth row
    int xPos = m_pTableView->columnViewportPosition( 2 ) + 5;
    int yPos = m_pTableView->rowViewportPosition( 3 ) + 10;
    
    // Retrieve the viewport of the table view
    QWidget* pViewport = m_pTableView->viewport();
    
    // Double click the table cell to set it into editor mode
    // Note: A simple double click did not work, Click->Double Click works, however
    QTest::mouseClick ( pViewport, Qt::LeftButton, NULL, QPoint( xPos, yPos ) );
    QTest::mouseDClick( pViewport, Qt::LeftButton, NULL, QPoint( xPos, yPos ) );
    
    // Simulate [End] keypress
    QTest::keyClick( pViewport->focusWidget(), Qt::Key_End );
    
    // Simulate [5] keypress
    QTest::keyClick( pViewport->focusWidget(), Qt::Key_5 );
    

    (Note: if you want to verify this, you can add QTest::qWait( 1000 ) commands after each event)


    If you are using the _data() functions as described here, note that you cannot retrieve the focusWidget() at the time of data creation.
    I solved this issue by creating a custom interface ITestAction with only a pure virtual “execute()” function. I then added subclasses with a similar constructor as the QTest::mouseClick(...) etc functions. These classes simply call the QTest functions but use either the widget itself or its focus widget as a parameter, dependent on an additional boolean flag.
    The _data() slot then stores a QList< ITestAction* > for each data row, and the actual test slot iterates over that list and calls execute() on each item before the validation is performed.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am reading a book about Javascript and jQuery and using one of the
I'm making a simple page using Google Maps API 3. My first. One marker
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
We're building an app, our first using Rails 3, and we're having to build
This could be a duplicate question, but I have no idea what search terms
We are using XSLT to translate a RIXML file to XML. Our RIXML contains

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.