Results 1 to 4 of 4

Thread: [SOLVED] Quit the program by pressing ESC

  1. #1
    Join Date
    Jul 2010
    Posts
    16
    Thanks
    3
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default [SOLVED] Quit the program by pressing ESC

    Hi,

    I wrote this:
    Qt Code:
    1. void MainWindow::quitProgram(QKeyEvent *event)
    2. {
    3. if(event->key() == Qt::Key_Escape)
    4. qApp->quit();
    5. }
    To copy to clipboard, switch view to plain text mode 
    but it doesn't work.
    Where I'm wrong?
    Thanks.
    Last edited by ale6111; 26th July 2010 at 14:47.

  2. #2
    Join Date
    May 2010
    Location
    Romania
    Posts
    1,021
    Thanks
    62
    Thanked 260 Times in 246 Posts
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: Quit the program by pressing ESC

    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:
    Qt Code:
    1. QObject::connect(&MainWindowObject, SIGNAL(closeExe()), &QApplicationObject, SLOT(quit()));
    To copy to clipboard, switch view to plain text mode 
    Last edited by Zlatomir; 26th July 2010 at 14:29.

  3. #3
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: Quit the program by pressing ESC

    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.

  4. The following 2 users say thank you to Lykurg for this useful post:

    ale6111 (26th July 2010), Zlatomir (26th July 2010)

  5. #4
    Join Date
    Jul 2010
    Posts
    16
    Thanks
    3
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Quit the program by pressing ESC

    Yes it works, it was what I wanted to do, my mistake . Thanks.

Similar Threads

  1. QWizard: avoid to go next when pressing enter [Qt4.5]
    By kalos80 in forum Qt Programming
    Replies: 2
    Last Post: 3rd June 2009, 17:11
  2. Replies: 2
    Last Post: 8th October 2008, 14:18
  3. Quit, Exit qApp (program) if error?
    By Arsenic in forum Newbie
    Replies: 13
    Last Post: 30th September 2008, 12:59
  4. Dialog closes on pressing Esc key?
    By vishal.chauhan in forum Qt Programming
    Replies: 6
    Last Post: 3rd July 2008, 08:53
  5. closing of window on pressing ESC key
    By raghvendramisra in forum Newbie
    Replies: 2
    Last Post: 7th January 2008, 02:36

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.