Results 1 to 2 of 2

Thread: Event Filter doesnt work as expected

  1. #1
    Join Date
    Sep 2012
    Location
    Iran
    Posts
    34
    Thanks
    33
    Thanked 2 Times in 2 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Event Filter doesnt work as expected

    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
    Qt Code:
    1. #ifndef MAINWINDOW_H
    2. #define MAINWINDOW_H
    3.  
    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. explicit MainWindow(QWidget *parent = 0);
    16. ~MainWindow();
    17.  
    18.  
    19. protected:
    20. bool eventFilter( QObject* sender, QEvent* event);
    21.  
    22. private:
    23. Ui::MainWindow *ui;
    24. };
    25.  
    26. #endif // MAINWINDOW_H
    To copy to clipboard, switch view to plain text mode 

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

  2. #2
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Event Filter doesnt work as expected

    From the friendly QObject::eventFilter() docs:
    Filters events if this object has been installed as an event filter for the watched object.
    So, where have you installed this QObject as an event filter (QObject::installEventFilter())? You use this if you want to see all the events destined for another QObject before that object sees them.

    If you want to see the key press events that make it to this QWidget then you want to reimplement QWidget::keyPressEvent().
    If you want to see all events then reimplement QWidget::event().

Similar Threads

  1. Shortcut doesnt work
    By tuli in forum Qt Programming
    Replies: 0
    Last Post: 18th September 2012, 15:01
  2. QT_TR_NOOP doesnt work
    By vhptt in forum Newbie
    Replies: 1
    Last Post: 1st August 2012, 09:17
  3. Event filter: very confused about how they work
    By papillon in forum Qt Programming
    Replies: 8
    Last Post: 20th November 2011, 00:46
  4. Qwt Tex Text Doesnt work for me
    By gbmtoday in forum Qwt
    Replies: 1
    Last Post: 21st November 2010, 19:48
  5. How come this doesnt work?
    By ShaChris23 in forum Newbie
    Replies: 8
    Last Post: 16th June 2007, 04:43

Tags for this Thread

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.