10

I'm writing a test app which simulates key presses and I would like to get what window is displayed after each key presses. Here's the code block.

std::auto_ptr<MyForm> pForm(new MyForm(3,3)); 
QTest::keyPress(pForm.get(), Qt::Key_0); 

After pressing 0 here, A window is gonna show up and I would like to check what window it is so I could QCompare/evaluate it later.

Any Ideas?

Updated:

I'm getting a segmentation fault when I use

std::auto_ptr<MyForm> pForm(new MyForm(3,3)); 
QTest::keyPress(pForm.get(), Qt::Key_0); 
QWidget *pWin = QApplication::activeWindow();
QCOMPARE(pWin->windowTitle(), QString("My Second Menu"));
Venemo
  • 18,515
  • 13
  • 84
  • 125
Owen
  • 4,063
  • 17
  • 58
  • 78

1 Answers1

11

If all your windows have been created through your application, you can use the QApplication class. By example, the activeWindow() function returns the widget that have the input focus. But there's a lot of other functions that can help you.

Hope that helps

Patrice Bernassola
  • 14,136
  • 6
  • 46
  • 59
  • I'm getting a segmentation fault though. I might be doing something wrong... I updated the code block above... :( – Owen Nov 18 '10 at 10:45
  • 5
    As explain in the documentation, the return pointer can be null if no window has the input focus. This can happened where widow has no keyboard input widget. Try to add a QTextEdit in your test windows. – Patrice Bernassola Nov 18 '10 at 10:51
  • The problem is, the window that appears after each key press is a Form with a Menu... I can't just add a qtextedit or just modify the application that I need to test... :( . Is there any other way to get the pointer of the currently active window? – Owen Nov 19 '10 at 02:38
  • 1
    Is it because there isn't really any activated window when you're just simulating? – Owen Nov 19 '10 at 07:46
  • An active window is a window that has the keyboard input focus. Try to set focus to the window or menu before calling activeWindow(). – Patrice Bernassola Nov 19 '10 at 08:51
  • links are dead. – Manthan Tilva Jan 11 '17 at 10:03