I tried to override the keypressevent for my lineEdit control using eventFiltering.
but when i run my application it doesnt do anything! i even set a break point inside the eventFiltering method , but i never get there!!?
This is my source codes :
MainWindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
namespace Ui {
class MainWindow;
}
{
Q_OBJECT
public:
explicit MainWindow
(QWidget *parent
= 0);
~MainWindow();
protected:
private:
Ui::MainWindow *ui;
};
#endif // MAINWINDOW_H
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
protected:
bool eventFilter( QObject* sender, QEvent* event);
private:
Ui::MainWindow *ui;
};
#endif // MAINWINDOW_H
To copy to clipboard, switch view to plain text mode
MainWindow.cpp:
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "QMessageBox"
#include <QKeyEvent>
MainWindow
::MainWindow(QWidget *parent
) : ui(new Ui::MainWindow)
{
ui->setupUi(this);
}
{
if (sender == ui->lineEdit)
{
if(event
->type
()== QEvent::KeyPress) {
if( keyEvent->key() == Qt::Key_Control)
{
return true;
}else
{
return false;
}
}
}
return QWidget::eventFilter(sender,event
);
}
MainWindow::~MainWindow()
{
delete ui;
}
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "QMessageBox"
#include <QKeyEvent>
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
}
bool MainWindow::eventFilter(QObject *sender, QEvent *event)
{
if (sender == ui->lineEdit)
{
if(event->type()== QEvent::KeyPress)
{
QKeyEvent * keyEvent = (QKeyEvent*)(event);
if( keyEvent->key() == Qt::Key_Control)
{
QMessageBox::information(this,"Salam","Test");
return true;
}else
{
return false;
}
}
}
return QWidget::eventFilter(sender,event);
}
MainWindow::~MainWindow()
{
delete ui;
}
To copy to clipboard, switch view to plain text mode
Bookmarks