在QListWidget上QTest :: mouseClick的问题

时间:2012-05-23 21:47:16

标签: qtestlib

我正在尝试使用QTest进行一些测试。我有一个QListWidget,我想点击它来获取选择。但点击后,没有选择任何内容。有没有人有任何想法?

这是我的测试类

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" ) );
}

以下是测试类

class TestGui: public QObject {
    Q_OBJECT

private slots:
    void List();
};

这是用于显示问题的TestDialog类

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

1 个答案:

答案 0 :(得分:7)

经过一番思考,事实证明点击需要在视图小部件上,而不是列表本身。所以该行应该看起来像这样

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

由于