Search code examples
user-interfacekeyboard-eventsqt6qttest

How do I use QTest::keyClick with control characters?


I am trying to simulate user input to close a window. I try to use QTest::keyClick(Qt::key::Key_F4, myWidget, Qt::KeyboardModifiers{ Qt::KeyboardModifier::AltModifier }); to simulate the user clicking the Alt+F4 keyboard combo, but it does not work. I step through the code and it seems like keyClick and similar functions only support ASCII characters. Is there a way to simulate keyClicks that are control characters?

For more detail into my use case, I want to pop-up a message box when someone closes the window. I have an event filter that handles this. I have code to check whether the message box is created, but the test case fails because the event filter fails to determine the event. I think it has something to do with what I pass into keyClick, but I am unsure.


Solution

  • I was testing behavior that my code ultimately did not support. My eventFilter() method checked for events of type KeyPress, KeyRelease, and Close. QTest::keyClick() does not trigger any of those. The way I fixed it was to call QTest::keyPress() to simulate pressing the Alt key and then call QWidget::close() to simulate closing the widget. This change in the code made my test pass.