Results 1 to 19 of 19

Thread: Eventfilter problem

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Apr 2009
    Posts
    206
    Thanks
    34
    Thanked 2 Times in 2 Posts

    Default Eventfilter problem

    Hello friends,

    I have a subclassed qtextedit in a QMAinwindow. So my target is to achieve that the key events from textedit
    are handled in Mainwindow event system. so when I make this:
    Qt Code:
    1. //**************************************************************************
    2. class keyPressCatcher:public QObject
    3. //**************************************************************************
    4. {
    5. public:
    6. keyPressCatcher():QObject()
    7. {};
    8. ~keyPressCatcher()
    9. {};
    10.  
    11. bool eventFilter(QObject* object,QEvent* event)
    12. {
    13. if (event->type() == QEvent::KeyPress)
    14. {
    15. QKeyEvent *keyEvent = dynamic_cast<QKeyEvent *>(event);
    16.  
    17. std::cout << "You Pressed " << keyEvent->text().toStdString() << "\n";
    18. return true;
    19. }
    20. else
    21. {
    22. // standard event processing
    23. return QObject::eventFilter(object, event);
    24. }
    25.  
    26.  
    27.  
    28. };
    29.  
    30.  
    31. };
    To copy to clipboard, switch view to plain text mode 

    and install the eventfilter

    Qt Code:
    1. this->installEventFilter(new keyPressCatcher());
    2.  
    3. and textedit->installEventFilter(this);
    To copy to clipboard, switch view to plain text mode 

    nothing appears.

    So my question is what the the basic step to achieve this functionality?

    Thanx in advance

  2. #2
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Eventfilter problem

    So my target is to achieve that the key events from textedit
    are handled in Mainwindow event system.
    You are confusing things.
    You don't need the keyPressCatcher class.
    Just overload eventFilter() in your main window class and then you only need to install the main window as event filter:
    Qt Code:
    1. textedit->installEventFilter(this);
    To copy to clipboard, switch view to plain text mode 
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  3. #3
    Join Date
    Apr 2009
    Posts
    206
    Thanks
    34
    Thanked 2 Times in 2 Posts

    Default Re: Eventfilter problem

    Ok, I implement it as you suggest. Now when I hit Ctrl+E or whatever alpabetically concunjction I run into
    Qt Code:
    1. MainWindow::keyPressEvent(QKeyEvent* event)
    To copy to clipboard, switch view to plain text mode 

    but when I hit Enter in TExtedit I have no reaction.

  4. #4
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Eventfilter problem

    Ok, I implement it as you suggest. Now when I hit Ctrl+E or whatever alpabetically concunjction I run into
    How do you know you run into the key handler?
    Is because your code is doing something or by setting a break point?
    If the former, you are probably not using the correct key code.
    Note that there is Qt::Key_Return and Qt::Key_Enter.
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  5. #5
    Join Date
    Apr 2009
    Posts
    206
    Thanks
    34
    Thanked 2 Times in 2 Posts

    Default Re: Eventfilter problem

    Here are the event method:

    Qt Code:
    1. //**************************************************************************
    2. void MainWindow::keyPressEvent(QKeyEvent* event)
    3. //**************************************************************************
    4. {
    5. qDebug() << Q_FUNC_INFO;
    6. Qt::Key key;
    7. int keyInt(0);
    8. QKeyEvent *keyEvent;
    9. if (event->type() == QEvent::KeyPress)
    10. {
    11. keyEvent = static_cast<QKeyEvent*>(event);
    12. keyInt = keyEvent->key();
    13. key = static_cast<Qt::Key>(keyInt);
    14. if(key == Qt::Key_unknown){
    15. qDebug() << "Unknown key from a macro probably";
    16. return;
    17. }
    18. }
    19. // the user have clicked just and only the special keys Ctrl, Shift, Alt, Meta.
    20. if(key == Qt::Key_Control ||
    21. key == Qt::Key_Shift ||
    22. key == Qt::Key_Alt ||
    23. key == Qt::Key_Meta ||
    24. key == Qt::Key_Return ||
    25. key == Qt::Key_Enter )
    26. {
    27. qDebug() << "Single click of special key: Ctrl, Shift, Alt or Meta or Enter/Return";
    28. qDebug() << "New KeySequence:" << QKeySequence(keyInt).toString(QKeySequence::NativeText);
    29. return;
    30. }
    31. // check for a combination of user clicks
    32. Qt::KeyboardModifiers modifiers = keyEvent->modifiers();
    33. QString keyText = keyEvent->text();
    34. // if the keyText is empty than it's a special key like F1, F5, ...
    35. qDebug() << "Pressed Key:" << keyText;
    36.  
    37. if(modifiers & Qt::ShiftModifier)
    38. keyInt += Qt::SHIFT;
    39. if(modifiers & Qt::ControlModifier)
    40. keyInt += Qt::CTRL;
    41. if(modifiers & Qt::AltModifier)
    42. keyInt += Qt::ALT;
    43. if(modifiers & Qt::MetaModifier)
    44. keyInt += Qt::META;
    45.  
    46. qDebug() << "New KeySequence:" << QKeySequence(keyInt).toString(QKeySequence::NativeText);
    47.  
    48. }
    To copy to clipboard, switch view to plain text mode 

  6. #6
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Eventfilter problem

    and for enter you get nothing?
    What platform are you on?
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  7. #7
    Join Date
    Apr 2009
    Posts
    206
    Thanks
    34
    Thanked 2 Times in 2 Posts

    Default Re: Eventfilter problem

    Yes, no qDebug() output: I am on Win7 64Bit/ Qt 4.8.1 for Desktop - MSVC2010

  8. #8
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Eventfilter problem

    Yes, no qDebug() output:
    Oh, so that's ok then.
    What is the problem then?
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  9. #9
    Join Date
    Apr 2009
    Posts
    206
    Thanks
    34
    Thanked 2 Times in 2 Posts

    Default Re: Eventfilter problem

    When I hit Enter in TExtedit I would like to enter this section in KeypressEvent:

    Qt Code:
    1. if(key == Qt::Key_Control ||
    2. key == Qt::Key_Shift ||
    3. key == Qt::Key_Alt ||
    4. key == Qt::Key_Meta ||
    5. key == Qt::Key_Return ||
    6. key == Qt::Key_Enter )
    7. {
    8. qDebug() << "Single click of special key: Ctrl, Shift, Alt or Meta or Enter/Return";
    9. qDebug() << "New KeySequence:" << QKeySequence(keyInt).toString(QKeySequence::NativeText);
    10. return;
    11. }
    To copy to clipboard, switch view to plain text mode 

    But I have no output.

  10. #10
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Eventfilter problem

    But your event filter method is being called - so just step through and see where the code is going and why - you have to debug - its pars of software development.
    See what key you actually get, and that the code runs the way you actually meant.
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  11. #11
    Join Date
    Apr 2009
    Posts
    206
    Thanks
    34
    Thanked 2 Times in 2 Posts

    Default Re: Eventfilter problem

    Thats the problem. When I hit Ctrl+d or F1 or only Ctrl I run into my
    Qt Code:
    1. MainWindow::keyPressEvent(QKeyEvent* event)
    To copy to clipboard, switch view to plain text mode 
    But when I only press Enter I get a carriage return in my textedit but no event is triggered so I cant debug.

    So when I reimplement my Textedit::keyPressEvent

    and call
    Qt Code:
    1. if(e->key() == Qt::Key_Return)
    2. qDebug() << "Hallo";
    To copy to clipboard, switch view to plain text mode 

    I get my output;

    I provide an example project where you can see it if you can surely(when you have the time & fun). When I handle return in MAinwindow no event is triggered. When I reimplement it again in Textedit I can catch the event.
    I cannot add a zip archiv....
    Last edited by codeman; 3rd July 2012 at 15:23.

  12. #12
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Eventfilter problem

    But when I only press Enter I get a carriage return in my textedit but no event is triggered so I cant debug.
    Oh, I read to fast your previous answer, I thought you DO get a qDebug().

    But you can debug - set a break point in the first line of the function - that is the only sure way to know.

    In the information you provided I can't see anything else.
    Post you project, and i will try it.
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

Similar Threads

  1. EventFilter problem
    By aruval3 in forum Qt Programming
    Replies: 10
    Last Post: 17th November 2011, 21:22
  2. problem in eventFilter of QGraphicsView()
    By wagmare in forum Qt Programming
    Replies: 7
    Last Post: 20th July 2009, 12:28
  3. sceneEventFilter or eventFilter
    By zgulser in forum Qt Programming
    Replies: 7
    Last Post: 4th May 2009, 07:50
  4. eventFilter in Console App
    By KaptainKarl in forum Newbie
    Replies: 3
    Last Post: 20th December 2008, 00:27
  5. paintEvent in eventFilter
    By kernel_panic in forum Qt Programming
    Replies: 1
    Last Post: 10th October 2007, 20:59

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
  •  
Qt is a trademark of The Qt Company.