Search code examples
c++unit-testingqtqtestlib

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


Solution

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