Question

Below is a code block for simulating key presses on a menu. I can navigate deep into the menus of my application but when I get back to the main form, I'm getting a segmetation error.... :(

first I show my Application:

std::auto_ptr<MyForm> mainForm( new MyForm( 3, 3 ));
mainForm->show();

Then simulate a keyPress to show the main menu:

QTest::keyPress(mainForm.get(), Qt::Key_0, NULL, 300);
QTest::keyRelease(mainForm.get(), Qt::Key_0, NULL, 300);
QWidget *pWin = QApplication::activeWindow();
QCOMPARE(QString(pWin->objectName()), QString("MainMenu"));

Then simulate another keyPress to go back to my main application:

QTest::keyPress(pWin, Qt::Key_Escape, NULL, 300);
QTest::keyRelease(pWin, Qt::Key_Escape, NULL, 300);
pWin = QApplication::activeWindow();
QCOMPARE(QString(pWin->objectName()), QString("MainForm"));

This is where I'm getting the segmentation fault... Can anybody tell me what's causing this error?

Was it helpful?

Solution

Start with checking the result of QApplication::activeWindow(). From the docs..

Returns the application top-level window that has the keyboard input focus, or 0 if no application window has the focus. There might be an activeWindow() even if there is no focusWidget(), for example if no widget in that window accepts key events.

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