Problems with QTest::mouseClick on QListWidget

qtestlib

Solution

After some more thought, it turns out that the click needs to be on the view widget and not the list itself. So the line should look like this

QTest::mouseClick ( list->viewport (), Qt::LeftButton, 0, rect.center() );

Thanks

Problem

I am trying to use QTest to do some testing. I have a QListWidget that I would like to click on to get a selection. But after the click, nothing is selected. Does anyone have any ideas? Here is my test class ``` void TestGui::List() { TestDialog dlg; dlg.show (); // Click on the centre of the second object QListWidget *list = dlg.ListWidget (); QListWidgetItem *item = list->item ( 1 ); QRect rect = list->visualItemRect ( item ); QTest::mouseClick ( list, Qt::LeftButton, 0, rect.center() ); // Check if something was selected QCOMPARE ( list->currentRow (), 1 ); QVERIFY ( list->currentItem () != NULL ); QCOMPARE ( list->currentItem ()->text (), QString ( "Two" ) ); } ``` Below is the testing class ``` class TestGui: public QObject { Q_OBJECT private slots: void List(); }; ``` And here is the TestDialog class used to display the problem ``` class TestDialog : public QDialog { Q_OBJECT public: TestDialog ( QWidget *parent = NULL ) : QDialog ( parent, Qt::CustomizeWindowHint | Qt::WindowCloseButtonHint ) { QVBoxLayout *layout = new QVBoxLayout ( this ); m_list = new QListWidget ( this ); m_list->addItem ( "One" ); m_list->addItem ( "Two" ); m_list->addItem ( "Three" ); m_list->addItem ( "Four" ); layout->addWidget ( m_list ); QPushButton *close_button = new QPushButton( "Close" ); connect ( close_button, SIGNAL ( clicked () ), this, SLOT ( close () ) ); layout->addWidget ( close_button ); setWindowTitle( "Test" ); } QListWidget *ListWidget ( void ) { return m_list; }; private: QListWidget *m_list; }; // TestDialog ```

Original source