Event Filter doesnt work as expected [Solved]



  • 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;
    }

    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
    @

    MainWindow.cpp:

    @
    #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;
    }
    @


  • Lifetime Qt Champion

    Hi and welcome to devnet,

    You didn't install the eventFilter. Have a look at "this":http://qt-project.org/doc/qt-4.8/qobject.html#installEventFilter



  • Thank you very much :)


  • Lifetime Qt Champion

    You're welcome !

    Don't forget to update the thread's title to solved, other forum users will know that a solution has been found :)



  • done :)
    the subject is edited and solved is added :)


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.