Results 1 to 13 of 13

Thread: Problem with catching keyPress events ?

  1. #1
    Join Date
    Aug 2008
    Posts
    27
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Problem with catching keyPress events ?

    i just read the example code given in the Article "Early Easter Eggs" which is in one of the early Qt Quarterly s.

    İ derived my EasterEgg just the same as the one in the Article.

    İ defined a member of type MyEasterEgg class in my main class.

    and instantiated it in the constructor just as the same as told in the Article.

    However when i debug the program i realized that it catches every keypress event three times.
    What m i missing ?

    Is there stg i should do more than that is told in the Article..?

    thanks..

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Problem with catching keyPress events ?

    Maybe you are installing the event filter multiple times or something like that. We'd have to see the exact code.

  3. #3
    Join Date
    Aug 2008
    Posts
    27
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Problem with catching keyPress events ?

    Quote Originally Posted by wysota View Post
    Maybe you are installing the event filter multiple times or something like that. We'd have to see the exact code.

    Actually i discovered the problem exists only when my main window has a QTextBrowser or one of the ItemWidgets' instance and that instance has focus.

    when i tried with other widgets on the form it works.

    you are very right at that eventFilter() routine is called multiple times.
    But this happens only i placed those widgets ( QTextBrowser , QTreeWidet, QListWidget .. ). Without changing my code just removing these widgets problems disappears.

    Can these widgets make QApplication catch key events multiple times.


    thanks..

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Problem with catching keyPress events ?

    Hard to say, but I think it might be possible. You can modify your event filter so that it shows you the target object of the event to test what widgets should receive those multiplied events (one of them will probably be the viewport and another would be the view itself).

  5. #5
    Join Date
    Aug 2008
    Posts
    27
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Problem with catching keyPress events ?

    i made my MainWindow having only a QListWidget object.

    and inserted the below lines into my eventFilter function of my EasterEgg

    Qt Code:
    1. if (event->type() == QEvent::KeyPress )
    2. qDebug(obj->objectName().toLocal8Bit().constData());
    To copy to clipboard, switch view to plain text mode 

    and when i run the program and pressed single key it outputs below

    listWidget
    centralWidget
    MainWindowClass


    how this can happen although i only install my event filter to qApp' eventFilter, which is told in the Article ?

  6. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Problem with catching keyPress events ?

    Both the list widget and the central widget ignore() the event so it propagates further. I suggest you store information about handling an event in your easter egg so that you process each event once.

  7. #7
    Join Date
    Aug 2008
    Posts
    27
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Problem with catching keyPress events ?

    Quote Originally Posted by wysota View Post
    I suggest you store information about handling an event in your easter egg so that you process each event once.
    i think u mean storing information about the last processed event so that we process each event just once. m i right ?

    But QEvent class has no identity information to be stored. What can i store about the event instance coming to my eventFilter() ?

    it seems that ; as for the KeyPress event ; when i pressed the same key twice ,exactly the same event objects coming to my filter.

  8. #8
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Problem with catching keyPress events ?

    Quote Originally Posted by arbi View Post
    i think u mean storing information about the last processed event so that we process each event just once. m i right ?
    Yes, something like that.

    But QEvent class has no identity information to be stored. What can i store about the event instance coming to my eventFilter() ?
    At worst you can store the address of an event. But you should always be able to detect multiplicates just by expecting them.

    it seems that ; as for the KeyPress event ; when i pressed the same key twice ,exactly the same event objects coming to my filter.
    No, they are alike but not the same instances.

  9. #9
    Join Date
    Aug 2008
    Posts
    27
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Problem with catching keyPress events ?

    Quote Originally Posted by wysota View Post

    At worst you can store the address of an event.
    this is the first thing that i tried. but it didnt worked since exactly the same address is allocated forthe consecutive but different keyboard events.


    Quote Originally Posted by wysota View Post
    No, they are alike but not the same instances.
    i know that. but i couldnt find any thing to be used as instance-ID in the property list of QEvent class and the memory address included..


    may be the time of last processed event can be stored and checked but that seems too risky and and ugly to me.



    anyway already thanks for your care...

  10. #10
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Problem with catching keyPress events ?

    Quote Originally Posted by arbi View Post
    this is the first thing that i tried. but it didnt worked since exactly the same address is allocated forthe consecutive but different keyboard events.
    What code did you use? Maybe you compared pointers to the event and not the event itself?

  11. #11
    Join Date
    Aug 2008
    Posts
    27
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Problem with catching keyPress events ?

    yes, i am comparing pointers to the event objects. what else can i compare?
    the comparison operator for QEvent objects is not defined.

    may be i do not understand well what you do mean . Here is my code:

    Qt Code:
    1. bool MyEasterEgg::eventFilter(QObject* obj, QEvent* event)
    2. {
    3. if (event->type() = QEvent::KeyPress)
    4. {
    5. // mLastProcessedEvent is a member in MyEasterEgg
    6. if (event == mLastProcessedEvent )
    7. return false;
    8.  
    9. mLastProcessedEvent = event;
    10.  
    11. /* The rest is the code for my custom behaviour */
    12.  
    13. }
    14. return false;
    15. }
    To copy to clipboard, switch view to plain text mode 

  12. #12
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Problem with catching keyPress events ?

    Quote Originally Posted by arbi View Post
    yes, i am comparing pointers to the event objects. what else can i compare?
    You are getting a copy of a pointer to the event. You need to dereference it and ask for its address, something like &(*event) could work.

  13. #13
    Join Date
    Aug 2008
    Posts
    27
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Problem with catching keyPress events ?

    Quote Originally Posted by wysota View Post
    You are getting a copy of a pointer to the event. You need to dereference it and ask for its address, something like &(*event) could work.
    does nt those two lines of expressions always return the same address values ?

    &(*event)
    event

    (unless event is not a bad pointer or NULL, in which case access violation can happen for the former expression.)

    i guess the operators * and & are neutralising for each other..

    i tried that anyway; and did not worked...

Similar Threads

  1. Problem in Mouse Press Events & Vectors
    By dheeraj in forum Qt Programming
    Replies: 2
    Last Post: 5th July 2008, 18:08
  2. problem about mouse button events
    By vql in forum Qt Programming
    Replies: 1
    Last Post: 29th April 2008, 10:14
  3. Strange problem with events on Unix
    By sukanyarn in forum Qt Programming
    Replies: 2
    Last Post: 7th November 2006, 02:45
  4. Problem with receiving events from QDateEdit
    By gunhelstr in forum Qt Programming
    Replies: 4
    Last Post: 20th April 2006, 11:21
  5. Replies: 16
    Last Post: 7th March 2006, 15:57

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.