PDA

View Full Version : Close main window with ESC Key



h3xl3y
2nd September 2013, 17:16
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

#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QtGui>
#include <QObject>
#include <QEvent>
#include <QKeyEvent>


MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
installEventFilter(this);
}

MainWindow::~MainWindow()
{
delete ui;
}

bool MainWindow::eventFilter(QObject *target, QEvent *event)
{
if (event->type() == QEvent::KeyPress)
{
QKeyEvent *keyEvent = static_cast<QKeyEvent*>(event);

if (keyEvent->key() == Qt::Key_Escape)
{
this->close();
return QMainWindow::eventFilter(target,event);
}
}
return QMainWindow::eventFilter(target,event);
}


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

Thanks

Infinity
2nd September 2013, 19:03
Show you header file. Maybe the declaration of eventFilter is missing there.

h3xl3y
2nd September 2013, 20:08
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

Infinity
2nd September 2013, 20:15
Add the declaration of the method

bool MainWindow::eventFilter(QObject *target, QEvent *event);
to the public section of the header file.

h3xl3y
2nd September 2013, 20:34
it gives me this error:

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

Infinity
2nd September 2013, 20:44
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.

h3xl3y
2nd September 2013, 21:33
if it should be like this then it doesn't work

#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QKeyEvent>
#include <QMainWindow>

namespace Ui {
class MainWindow;
}

class MainWindow : public QMainWindow
{
Q_OBJECT

public:
bool eventFilter(QObject *parent, QEvent *event);
explicit MainWindow(QWidget *parent = 0);
~MainWindow();

private:
Ui::MainWindow *ui;

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

#endif // MAINWINDOW_H

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

Infinity
2nd September 2013, 22:42
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.


class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();

private:
Ui::MainWindow *ui;

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

void MainWindow::keyPressEvent(QKeyEvent *event) // definition
{
switch(event->key())
{
case Qt::Key_Escape:
close();
break;
default:
QMainWindow::keyPressEvent(event);
}
}

Gokulnathvc
3rd September 2013, 08:05
Use this
bool MainWindow::keypress(QKeyEvent *keyevent)
{
if (keyevent->key()==Qt::Key_W) // Your key here.
{
qApp::Quit();
}
}

h3xl3y
4th September 2013, 10:43
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

bool keypress(QKeyEvent *keyevent)
i get this error: expected ';' at end of member declaration
if add ; then expected ';' before '::' token

wysota
4th September 2013, 14:24
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.

h3xl3y
4th September 2013, 14:39
Ok... Then, if its possible, can you give me a hand ?

wysota
4th September 2013, 14:53
You could install the event filter on the applicaytion object to intercept all events passing through the program.

Gokulnathvc
4th September 2013, 14:54
Just declare
void keyPressEvent(QKeyEvent *); in header file. And use it as
void MainWindow::keyPressEvent(QKeyEvent *event)
{
if(event->key() == Qt::Key_Escape)
{
myLabel->setText("You pressed ESC");
}
}

h3xl3y
4th September 2013, 15:25
Just declare
void keyPressEvent(QKeyEvent *); in header file. And use it as
void MainWindow::keyPressEvent(QKeyEvent *event)
{
if(event->key() == Qt::Key_Escape)
{
myLabel->setText("You pressed ESC");
}
}

Ok THanks. That help me a lot
i finally used this code

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

Infinity
5th September 2013, 01:54
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.

wysota
5th September 2013, 10:27
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.


#include <QtWidgets>

class MainWindow : public QMainWindow {
public:
MainWindow(QWidget *parent = 0) : QMainWindow(parent) {}
protected:
void keyPressEvent(QKeyEvent *ke) {
qDebug() << ke->key();
}
};

class Widget : public QWidget {
public:
Widget(QWidget *parent = 0) : QWidget(parent) {
setFocusPolicy(Qt::StrongFocus);
}
protected:
void keyPressEvent(QKeyEvent *ke) {}
};

int main(int argc, char **argv) {
QApplication app(argc, argv);
MainWindow mw;
Widget *w = new Widget;
mw.setCentralWidget(w);
mw.show();
return app.exec();
}

tsuibin
5th September 2013, 18:17
usethis function
void keyPressEvent(QKeyEvent *e)