PDA

View Full Version : Problem with catching keyPress events ?



arbi
28th August 2008, 12:00
i just read the example code given in the Article (http://doc.trolltech.com/qq/qq02-early-easter-eggs.html) "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..

wysota
28th August 2008, 14:01
Maybe you are installing the event filter multiple times or something like that. We'd have to see the exact code.

arbi
28th August 2008, 14:16
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..

wysota
28th August 2008, 14:41
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).

arbi
29th August 2008, 06:45
i made my MainWindow having only a QListWidget object.

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


if (event->type() == QEvent::KeyPress )
qDebug(obj->objectName().toLocal8Bit().constData());

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 ?

wysota
29th August 2008, 08:24
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.

arbi
29th August 2008, 11:15
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.

wysota
30th August 2008, 13:37
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.

arbi
1st September 2008, 06:22
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.




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...

wysota
1st September 2008, 09:14
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?

arbi
1st September 2008, 11:15
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:



bool MyEasterEgg::eventFilter(QObject* obj, QEvent* event)
{
if (event->type() = QEvent::KeyPress)
{
// mLastProcessedEvent is a member in MyEasterEgg
if (event == mLastProcessedEvent )
return false;

mLastProcessedEvent = event;

/* The rest is the code for my custom behaviour */

}
return false;
}

wysota
1st September 2008, 11:54
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.

arbi
1st September 2008, 12:35
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...