Question

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

Was it helpful?

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

OTHER TIPS

The best way is to set name for subobjects of tested widget and then fetch them by this name.

You should also try to give focus to this editor. There are many ways to do that. For example by calling nextInFocusChain until you reach desired editor and the calling setFocus(Qt::MouseFocusReason);

Other way to get private parts is sub classing and/or forked build for testing where you can freely access private parts (it is possible to prepare some macros for that).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top