1

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 controls 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...

Owen
  • 4,063
  • 17
  • 58
  • 78

2 Answers2

1

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...

Raiv
  • 5,731
  • 1
  • 33
  • 51
  • I tried getting the list of buttons and setting focus to any of the buttons. But passing Qt::Enter doesn't do anything... :( – Owen Jan 26 '11 at 03:48
  • QCOMPARE(QString(pWin->objectName()), QString("MyMainForm")); - this is the main window... maybe you need to get your popup window id and send command to it? Or you just not posted that piece of code? – Raiv Jan 26 '11 at 09:34
  • whenever I call QTest::keyPress(), I'm already sending a keypress to the window. – Owen Jan 27 '11 at 06:54
1

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.

Alexis Wilke
  • 19,179
  • 10
  • 84
  • 156