PDA

View Full Version : [SOLVED] Quit the program by pressing ESC



ale6111
26th July 2010, 14:58
Hi,

I wrote this:


void MainWindow::quitProgram(QKeyEvent *event)
{
if(event->key() == Qt::Key_Escape)
qApp->quit();
}

but it doesn't work.
Where I'm wrong?
Thanks.

Zlatomir
26th July 2010, 15:23
You can't access the QApplication object from there (and the QApplication object might have a different name)

One possible solution is to emit a signal when the Esc has been presed (example: closeExe(); or EscPresed();... ) and connect (in main.cpp), something like:


QObject::connect(&MainWindowObject, SIGNAL(closeExe()), &QApplicationObject, SLOT(quit()));

Lykurg
26th July 2010, 15:30
Sine QApplication is a singleton class you can access it form everywhere with qApp. So one possibility is to use QWidget::keyPressEvent() and put your code there. Then it should work.

ale6111
26th July 2010, 15:45
Yes it works, it was what I wanted to do, my mistake :D. Thanks.