QTest - Unable to pass Qt::Enter to QPushButton
I'm creating an automated test application using QTest Library. I'm able to simulate key presses on the application except when it gets to a window having QDialogButtonBox (Save, and Cancel). Here's my sample code:
std::auto_ptr<MainForm> myForm( new MainForm( 3, 3 ));
myForm->show();
QTest::keyPress(myForm.get(), Qt::Key_0, NULL, 1000);
QTest::keyRelease(myForm.get(), Qt::Key_0, NULL, 100);
QWidget *pWin = QApplication::activeWindow();
QCOMPARE(QString(pWin->objectName()), QString("MyMainForm"));
now when it gets to the next window, it has several control开发者_开发知识库s where the input focus is on a text edit control. When I press Enter, it presses the "Save" button. So theoretically, if I should pass Qt::Enter to the Form, it should press the "Save" button as well. However when I try to pass a keyPress:
QTest::keyPress(pWin, Qt::Key_Enter, 1000);
nothing happens... what do you think is going on? I've tried setFocus() to the button but nothing happens as well...
in QDialogButtonBox you may get needed button with
QPushButton * QDialogButtonBox::button ( StandardButton which )
and then call it's SetFocus method. If you can't access QDialogButtonBox directly, you may get it with
QList<T> QObject::findChildren ( const QString & name = QString() )
or even get buttons themself with this method...
I think you need to send the key event to the button or line edit instead of the parent window.
QWidget *pWin = QApplication::activeWindow();
QTest::keyPress(pwin, Qt::Key_0, NULL, 1000);
QTest::keyRelease(pwin, Qt::Key_0, NULL, 100);
I have to say that the documentation is not clear, but it works for me this way.
精彩评论