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
) : ui(new Ui::MainWindow)
{
ui->setupUi(this);
installEventFilter(this);
}
MainWindow::~MainWindow()
{
delete ui;
}
{
if (event
->type
() == QEvent::KeyPress) {
QKeyEvent *keyEvent
= static_cast<QKeyEvent
*>
(event
);
if (keyEvent->key() == Qt::Key_Escape)
{
this->close();
}
}
}
#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);
}
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
Bookmarks