PDA

View Full Version : sceneEventFilter or eventFilter



zgulser
29th April 2009, 09:15
Hi,
I have a QGraphicsItem in which I want to monitor it to catch mouse press event. I could do it by reimplementing mouse press event. But I want to do it in a different way by using event filtering.

I first tried out eventFilter in my graphics item but the program didn't even enters the eventFilter() fucntion. First of all is that OK?

Secondly, should I use sceneEventFilter()? Could this method give what I wanna achieve?

Thanks in advance

wysota
29th April 2009, 10:29
The item has a sceneEventFilter() method not eventFilter(). And you have to install an event filter to use it. But I'd really suggest to use mousePressEvent(). Intercepting events for yourself in the event filter doesn't make much sense. Is there any particular reason why you don't want to implement the functionality in the event handler?

zgulser
29th April 2009, 11:08
No actually, but I wanted to see how it works. I tried it but I couldn't go in to the following block;

if( pEvent->type() == QEvent::MousePressEvent)
{
// some code
}

what's wrong with it?

wysota
29th April 2009, 14:06
Did you install an event filter first? Is there any code we could compile, run and see for ourselves?

zgulser
30th April 2009, 06:38
Sure...I installed the scene event filter. but maybe I did it wrongly.

In my graphics item class, I wrote the following code;

installSceneEventFilter(this);


By the way, I'm sorry that I couldn't post you any code since this is prohibited:(

wysota
30th April 2009, 09:15
I suggest you just forget about this experiment and handle events the regular way. How does your sceneEventFilter() method look like, by the way? Could we at least see its signature?

zgulser
30th April 2009, 11:43
void fooItem::sceneEventFilter( QGraphicsItem *pWatched, QEvent* pEvent)
{

if( watched == this)
{

if( pEvent->type() == QEvent::MousePressEvent)
{
cout<<"item pressed!"<<endl;
return TRUE;
}
else
{
return FALSE;
}

}

}

wysota
4th May 2009, 07:50
The method should return bool not void...