与QTEST ::点击鼠标上QListWidget问题(Problems with QTest::m

2019-09-17 11:07发布

我试图用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

Answer 1:

一些更多的思考之后,事实证明,点击需要是在视图窗口小部件,而不是列表本身。 太行应该是这样的

QTEST ::鼠标点击(列表 - >视口(),Qt的:: LeftButton,0,rect.center());

谢谢



文章来源: Problems with QTest::mouseClick on QListWidget
标签: qtestlib