Search code examples
qtqtestlib

QTest cannot pass events to child widgets


I'm writing some auto-test code using qtestlib. In the example code below:

#include <QtTest/QtTest>

QWidget *win = new QWidget;
QLabel *label = new QLabel("&what");
QLineEdit *le = new QLineEdit;
label->setBuddy(le);

QHBoxLayout *layout = new QHBoxLayout;
layout->addWidget(label);
layout->addWidget(le);
win->setLayout(layout);
win->show();

QTestEventList events1, events2;
events1.addKeyClick(Qt::Key_W, Qt::AltModifier);
events1.addKeyClicks("hello");
events1.addDelay(1000);
events1.simulate(win); // didn't type 'hello'

events2.addKeyClicks("world");
events2.addDelay(1000);
events2.simulate(le); // did type 'world'

I can't let the le get focus and type 'hello' by sending events to its parent widget win. But I can do that by sending events to le directly.

Things is that normally there are many private widget members in a custom QWidget class. And I can't simulate key/mouse events by sending the events to the instance. And of cause I can't send events to its private widgets. So what can I do to perform a auto-test to such class?

environment: Gentoo Linux KDE Qt-4.8


Solution

  • Found solution now for sure:

      QTest::keyClick(win, Qt::Key_F, Qt::AltModifier,500);
      QTest::keyClicks(win->focusWidget(),"blah");
      QTest::keyClick(win, Qt::Key_W, Qt::AltModifier,500);
      QTest::keyClicks(win->focusWidget(),"blah2");
    

    enter image description here

    Last parameter is delay and It is necessary (You should test it).