
Originally Posted by
wysota
You need to use the signal() call with SIGINT.
I figured that in the mean time, but thanks anyway. It works.
But I'm still stuck with the question: suppose I would want to stop the program if I hit a specific key, not ctrl-C. How do I do that? Polling std::cin from the main loop is ugly. There must be a way to intercept keypress events, right? I tried this:
{
if (evt
->type
() == QEvent::keyPress) {
if ( /* correct key pressed */ )
{
return true;
}
}
return QObject::eventFilter(obj, evt
);
}
bool MyApp
::event(QEvent *evt
) {
if (evt
->type
() == QEvent::keyPress) {
if ( /* correct key pressed */ )
{
return true;
}
}
}
bool MyApp::eventFilter(QObject *obj, QEvent *evt)
{
if (evt->type() == QEvent::keyPress)
{
if ( /* correct key pressed */ )
{
QCoreApplication::quit();
return true;
}
}
return QObject::eventFilter(obj, evt);
}
bool MyApp::event(QEvent *evt)
{
if (evt->type() == QEvent::keyPress)
{
if ( /* correct key pressed */ )
{
QCoreApplication::quit();
return true;
}
}
return QObject::event(evt);
}
To copy to clipboard, switch view to plain text mode
The class MyApp is derived from QCoreApplication. I tried to call installEventFilter(this) in its constructor (seems a bit weird to install itself as filter but I tried it anyway), but neither of the two listed methods is ever called with a keypress event, whether or not I call installEventFilter.
So what am I doing wrong?
Bookmarks