English 中文(简体)
与 QTest:: MouseClick 的 QList 部件有问题
原标题:Problems with QTest::mouseClick on QListWidget
  • 时间:2012-05-23 21:47:16
  •  标签:
  • qtestlib

我正在尝试使用 QTest 来做一些测试 。 我有一个 QL 部件, 我想点击它来获取选择 。 但是在点击后, 什么也没有被选中 。 有人有什么想法吗?

这是我的考试班

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

这是用来显示问题的测试Dialog 类

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
问题回答

经过一些进一步思考后,结果发现点击需要放在视图部件上,而不是列表本身。所以线条应该像这样看

QTest::mouseClick( 列表- gt; 视图port (), Qt: LeftButton, 0, 矩形. center () );

谢谢 谢谢





相关问题
QT: When to use QT::keyRelease

I m writing a test app that tests another application with a bunch of menus. I simulate keyPresses by using QT::keyPress. However, at some point it doesn t accept keyPresses anymore, until I learned ...

QT: simulated unit-testing

Is it possible to run an application and on top of that, you send keyPress events to the app and test the results using qtestlib? If yes, can somebody give me an example how to do it? If no, can ...

Qt: How do I get the currently running window?

I m writing a test app which simulates key presses and I would like to get what window is displayed after each key presses. Here s the code block. std::auto_ptr<MyForm> pForm(new MyForm(3,3)); ...

Qtestlib log window

I link my program against the qtestlib and include QTest. But then I get a log-window which prompts all the qDebugs, when the application starts even if I set the configuration to release or debug. Is ...

QT Qtestlib, Unit Test

If I were to create a unit test for class implementation using QTestlib ( trying to figure it out) how would I do it. (I know unit testing for the simple class below can be done other simple way I ...

How to use QCOMPARE Macro to compare events

I have MyWindow class which pops up a blank window, which accepts a mouse click, I need to unit test the mouse click event Code snippet: void TestGui::testGUI_data() { QTest::addColumn<...

QTestLib: Undocumented command line argument -chart

In Qt 4.6 QTestLib supports the command-line argument "-chart" (but this is undocumented). A report.html is created, however neither Firefox 3.6 nor IE8 are able to display anything but the headline "...

Help understanding QTest tutorials

In the tutorials for QTestLib, there are references to the files "testgui.moc" and "testqstring.moc" (one example can be found here: http://www.englishbreakfastnetwork.org/coverage/build/qt-copy/...

热门标签