Results 1 to 18 of 18

Thread: Close main window with ESC Key

  1. #1
    Join Date
    Sep 2013
    Posts
    7
    Qt products
    Qt5
    Platforms
    Windows

    Default Close main window with ESC Key

    Hi
    i am very new to Qt and programming, sorry for trouble but i searched for 2 days for a answer and found a few things but it didn't work

    running Qt Creator 2.8., compiler MinGW 4.8

    I am trying to make the Program Close when press on ESC_Key

    using this code in main window.cpp
    Qt Code:
    1. #include "mainwindow.h"
    2. #include "ui_mainwindow.h"
    3. #include <QtGui>
    4. #include <QObject>
    5. #include <QEvent>
    6. #include <QKeyEvent>
    7.  
    8.  
    9. MainWindow::MainWindow(QWidget *parent) :
    10. QMainWindow(parent),
    11. ui(new Ui::MainWindow)
    12. {
    13. ui->setupUi(this);
    14. installEventFilter(this);
    15. }
    16.  
    17. MainWindow::~MainWindow()
    18. {
    19. delete ui;
    20. }
    21.  
    22. bool MainWindow::eventFilter(QObject *target, QEvent *event)
    23. {
    24. if (event->type() == QEvent::KeyPress)
    25. {
    26. QKeyEvent *keyEvent = static_cast<QKeyEvent*>(event);
    27.  
    28. if (keyEvent->key() == Qt::Key_Escape)
    29. {
    30. this->close();
    31. return QMainWindow::eventFilter(target,event);
    32. }
    33. }
    34. return QMainWindow::eventFilter(target,event);
    35. }
    To copy to clipboard, switch view to plain text mode 

    it gaves an error: no 'bool MainWindow::eventFilter(QObject*,QEvent*) member function declared in class 'MainWindow'

    Thanks

  2. #2
    Join Date
    Oct 2012
    Posts
    132
    Thanks
    10
    Thanked 21 Times in 21 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows Android

    Default Re: Close main window with ESC Key

    Show you header file. Maybe the declaration of eventFilter is missing there.

  3. #3
    Join Date
    Sep 2013
    Posts
    7
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Close main window with ESC Key

    here is the Header
    #ifndef MAINWINDOW_H
    #define MAINWINDOW_H
    #include <QKeyEvent>
    #include <QMainWindow>

    namespace Ui {
    class MainWindow;
    }

    class MainWindow : public QMainWindow
    {
    Q_OBJECT

    public:
    explicit MainWindow(QWidget *parent = 0);
    ~MainWindow();

    private:
    Ui::MainWindow *ui;

    protected:
    void keyPressEvent(QKeyEvent *event);
    };

    #endif // MAINWINDOW_H

  4. #4
    Join Date
    Oct 2012
    Posts
    132
    Thanks
    10
    Thanked 21 Times in 21 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows Android

    Default Re: Close main window with ESC Key

    Add the declaration of the method
    Qt Code:
    1. bool MainWindow::eventFilter(QObject *target, QEvent *event);
    To copy to clipboard, switch view to plain text mode 
    to the public section of the header file.

  5. #5
    Join Date
    Sep 2013
    Posts
    7
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Close main window with ESC Key

    it gives me this error:

    extra qualification 'MainWindow::' on member 'eventFilter' [-fpermissive]

  6. #6
    Join Date
    Oct 2012
    Posts
    132
    Thanks
    10
    Thanked 21 Times in 21 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows Android

    Default Re: Close main window with ESC Key

    You can't simply copy & paste the code I've posted. Of course you have to remove the "MainWindow::"-prefix, because you use declare the method inside "the class block".

    I recommend you, to lean the basics of C++ before using complex libraries like Qt.

  7. #7
    Join Date
    Sep 2013
    Posts
    7
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Close main window with ESC Key

    if it should be like this then it doesn't work
    Qt Code:
    1. #ifndef MAINWINDOW_H
    2. #define MAINWINDOW_H
    3. #include <QKeyEvent>
    4. #include <QMainWindow>
    5.  
    6. namespace Ui {
    7. class MainWindow;
    8. }
    9.  
    10. class MainWindow : public QMainWindow
    11. {
    12. Q_OBJECT
    13.  
    14. public:
    15. bool eventFilter(QObject *parent, QEvent *event);
    16. explicit MainWindow(QWidget *parent = 0);
    17. ~MainWindow();
    18.  
    19. private:
    20. Ui::MainWindow *ui;
    21.  
    22. protected:
    23. void keyPressEvent(QKeyEvent *event);
    24. };
    25.  
    26. #endif // MAINWINDOW_H
    To copy to clipboard, switch view to plain text mode 

    error: undefined reference to 'MainWindow::keyPressEvent(QKeyEvent*)'
    file not found: moc_mainwindow.cpp

    and you know that i'm trying to learn as i go but there is much info to absorb and not very clear when search for errors
    Last edited by h3xl3y; 2nd September 2013 at 22:37.

  8. #8
    Join Date
    Oct 2012
    Posts
    132
    Thanks
    10
    Thanked 21 Times in 21 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows Android

    Default Re: Close main window with ESC Key

    You also declare the protected method keyPressEvent, but it seems like the definition is missing. That's why you get the error. (There's only a definition for eventFilter, which had no declaration at the first time).

    I am trying to make the Program Close when press on ESC_Key
    I think, overwriting keyPressEvent (and not eventFilter) is the right way to solve your problem (so don't use the eventFilter method for that).

    The following code should work.
    Qt Code:
    1. class MainWindow : public QMainWindow
    2. {
    3. Q_OBJECT
    4. public:
    5. explicit MainWindow(QWidget *parent = 0);
    6. ~MainWindow();
    7.  
    8. private:
    9. Ui::MainWindow *ui;
    10.  
    11. protected:
    12. void keyPressEvent(QKeyEvent *event); // declaration
    13. };
    14.  
    15. void MainWindow::keyPressEvent(QKeyEvent *event) // definition
    16. {
    17. switch(event->key())
    18. {
    19. case Qt::Key_Escape:
    20. close();
    21. break;
    22. default:
    23. QMainWindow::keyPressEvent(event);
    24. }
    25. }
    To copy to clipboard, switch view to plain text mode 

  9. #9
    Join Date
    Mar 2011
    Location
    Coimbatore,TamilNadu,India
    Posts
    382
    Thanks
    10
    Thanked 13 Times in 12 Posts
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Close main window with ESC Key

    Use this
    Qt Code:
    1. bool MainWindow::keypress(QKeyEvent *keyevent)
    2. {
    3. if (keyevent->key()==Qt::Key_W) // Your key here.
    4. {
    5. qApp::Quit();
    6. }
    7. }
    To copy to clipboard, switch view to plain text mode 

  10. #10
    Join Date
    Sep 2013
    Posts
    7
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Close main window with ESC Key

    Sorry could not answer sooner

    @Infinity
    with the last code it gives this error: undeterminated #ifndef MAINWINDOW_H

    @Gokulnathvc
    with that code it still gives bool error: no 'bool MainWindow::keypress(QKeyEvent*) member function declared in class 'MainWindow'
    and if i put this in header
    Qt Code:
    1. bool keypress(QKeyEvent *keyevent)
    To copy to clipboard, switch view to plain text mode 
    i get this error: expected ';' at end of member declaration
    if add ; then expected ';' before '::' token

  11. #11
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Close main window with ESC Key

    None of the code posted so far in this thread has any chance to work because the main window object doesn't accept focus so it will receive no key events.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  12. #12
    Join Date
    Sep 2013
    Posts
    7
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Close main window with ESC Key

    Ok... Then, if its possible, can you give me a hand ?

  13. #13
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Close main window with ESC Key

    You could install the event filter on the applicaytion object to intercept all events passing through the program.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  14. #14
    Join Date
    Mar 2011
    Location
    Coimbatore,TamilNadu,India
    Posts
    382
    Thanks
    10
    Thanked 13 Times in 12 Posts
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Close main window with ESC Key

    Just declare
    Qt Code:
    1. void keyPressEvent(QKeyEvent *);
    To copy to clipboard, switch view to plain text mode 
    in header file. And use it as
    Qt Code:
    1. void MainWindow::keyPressEvent(QKeyEvent *event)
    2. {
    3. if(event->key() == Qt::Key_Escape)
    4. {
    5. myLabel->setText("You pressed ESC");
    6. }
    7. }
    To copy to clipboard, switch view to plain text mode 

  15. #15
    Join Date
    Sep 2013
    Posts
    7
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Close main window with ESC Key

    Quote Originally Posted by Gokulnathvc View Post
    Just declare
    Qt Code:
    1. void keyPressEvent(QKeyEvent *);
    To copy to clipboard, switch view to plain text mode 
    in header file. And use it as
    Qt Code:
    1. void MainWindow::keyPressEvent(QKeyEvent *event)
    2. {
    3. if(event->key() == Qt::Key_Escape)
    4. {
    5. myLabel->setText("You pressed ESC");
    6. }
    7. }
    To copy to clipboard, switch view to plain text mode 
    Ok THanks. That help me a lot
    i finally used this code
    Qt Code:
    1. void MainWindow::keyPressEvent(QKeyEvent *event)
    2. {
    3. if(event->key() == Qt::Key_Escape)
    4. qApp->quit();
    5. }
    To copy to clipboard, switch view to plain text mode 

  16. #16
    Join Date
    Oct 2012
    Posts
    132
    Thanks
    10
    Thanked 21 Times in 21 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows Android

    Default Re: Close main window with ESC Key

    None of the code posted so far in this thread has any chance to work because the main window object doesn't accept focus so it will receive no key events.
    I've tested my example code and it worked (using Qt 5.1.0 with QGtkTheme on Linux Mint).

    with the last code it gives this error: undeterminated #ifndef MAINWINDOW_H
    I don't know how you used the code, but if you use it properly there shouldn't occur errors.

  17. #17
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Close main window with ESC Key

    Quote Originally Posted by Infinity View Post
    I've tested my example code and it worked (using Qt 5.1.0 with QGtkTheme on Linux Mint).
    Only if the widget currently with focus does not handle the particular key event.

    Qt Code:
    1. #include <QtWidgets>
    2.  
    3. class MainWindow : public QMainWindow {
    4. public:
    5. MainWindow(QWidget *parent = 0) : QMainWindow(parent) {}
    6. protected:
    7. void keyPressEvent(QKeyEvent *ke) {
    8. qDebug() << ke->key();
    9. }
    10. };
    11.  
    12. class Widget : public QWidget {
    13. public:
    14. Widget(QWidget *parent = 0) : QWidget(parent) {
    15. setFocusPolicy(Qt::StrongFocus);
    16. }
    17. protected:
    18. void keyPressEvent(QKeyEvent *ke) {}
    19. };
    20.  
    21. int main(int argc, char **argv) {
    22. QApplication app(argc, argv);
    23. MainWindow mw;
    24. Widget *w = new Widget;
    25. mw.setCentralWidget(w);
    26. mw.show();
    27. return app.exec();
    28. }
    To copy to clipboard, switch view to plain text mode 
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  18. #18
    Join Date
    Apr 2009
    Posts
    35
    Thanks
    9
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Android

    Default Re: Close main window with ESC Key

    usethis function
    void keyPressEvent(QKeyEvent *e)

Similar Threads

  1. Replies: 2
    Last Post: 17th February 2011, 13:30
  2. main window close not working
    By hvranic in forum Newbie
    Replies: 2
    Last Post: 17th June 2010, 22:12
  3. Replies: 9
    Last Post: 16th May 2010, 17:21
  4. Replies: 11
    Last Post: 11th August 2008, 10:14
  5. Replies: 15
    Last Post: 23rd March 2007, 17:16

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.