Qt: How do I get the currently running window?

13,619

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

Share:
13,619
Owen
Author by

Owen

absorbs what is useful... discards what is not

Updated on July 20, 2022

Comments

  • Owen
    Owen almost 2 years

    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"));
    
  • Owen
    Owen over 13 years
    I'm getting a segmentation fault though. I might be doing something wrong... I updated the code block above... :(
  • Patrice Bernassola
    Patrice Bernassola over 13 years
    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.
  • Owen
    Owen over 13 years
    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
    Owen over 13 years
    Is it because there isn't really any activated window when you're just simulating?
  • Patrice Bernassola
    Patrice Bernassola over 13 years
    An active window is a window that has the keyboard input focus. Try to set focus to the window or menu before calling activeWindow().
  • Manthan Tilva
    Manthan Tilva over 7 years
    links are dead.