PDA

View Full Version : EventFilter and MouseMoveEvent



mickey
25th January 2006, 12:13
Hi, I installed an event filter on a PushButton in MainForm; Pushbutton call an QFileDialog
in wich I can choose file. If I choose a file with doubleClick, QFileDIalog is closing but
start MYWidget::MouseMoveEvent.


loadButton->installEventFilter(myWidget1);


I tried to filter out only mosue events but filtering is not executed. If I chancge below
QMouseEvent* in QEvent*: it works and filters out some event but none QMouseEvent.
I tried to not remove filter on loadButton and don't change....


bool MyWidget::eventFilter(QObject* obj, QMouseEvent* e) {
cout << "filter\n";
//if (obj == w->loadTexture) {
cout << e->type() << endl;
if (( e->type() != QMouseEvent::None)) {
cout << "filter out\n";
return TRUE;
}
else {
return FALSE;
}
}

:confused: thanks

wysota
25th January 2006, 12:56
bool MyWidget::eventFilter(QObject* obj, QMouseEvent* e)

The event filter method must use QEvent and not QMouseEvent. It is one of the methods of QWidget (or even QObject) and you are overloading it here, you can't create new methods with the same name and different arguments, because it will simply not be called. This isn't any "magic", it's plain C++.

If you wish to filter mouse events, you have to compare the type of the event received to the type you wish to check and then react.


bool MyWidget::eventFilter(QObject* obj, QEvent* e) {
cout << e->type() << endl;
if (( e->type() == QEvent::MouseMove)) {
cout << "filter out\n";
return TRUE;
}
return QWidget::eventFilter(obj,e);
}

Remember this will reject ANY mouse move event. You should check for the object too and be sure you really want to handle a particular event in the filter.

mickey
25th January 2006, 16:04
bool MyWidget::eventFilter(QObject* obj, QMouseEvent* e)

If you wish to filter mouse events, you have to compare the type of the event received to the type you wish to check and then react.


bool MyWidget::eventFilter(QObject* obj, QEvent* e) {
cout << e->type() << endl;
if (( e->type() == QEvent::MouseMove)) {
cout << "filter out\n";
return TRUE;
}
return QWidget::eventFilter(obj,e);
}

I proved to change QMouseEvent in QEvent but don't change.

cout << e->type() << endl;
This above says me wich types of events filter out: Paint = 12,FocusIn = 8...but
don't appear: MouseButtonPress = 2, MouseButtonRelease = 3, MouseButtonDblClick = 4,MouseMove = 5.
And how MouseMove is Called?I know is called because I insert a printf in MouseMoveEvent.....

mickey
25th January 2006, 22:46
Hi,
I proved to change QMouseEvent in QEvent (such as below) but don't change.


bool MyWidget::eventFilter(QObject* obj, QEvent* e) {
cout << e->type() << endl;
if (( e->type() == QEvent::MouseMove)) {
cout << "filter out\n";
return TRUE;
}
return QWidget::eventFilter(obj,e);
}

cout << e->type() << endl;
This above says me wich types of events are passed in eventfilter: Paint = 12,FocusIn = 8...but don't appear: MouseButtonPress = 2, MouseButtonRelease = 3, MouseButtonDblClick = 4,MouseMove = 5.
And how MouseMove is Called?I know is called because I insert a printf in MouseMoveEvent....:confused:
I don't understand why MouseMove is not filter out (mousemove don't pass in eventFilter....)
Thanks

axeljaeger
25th January 2006, 22:55
How do you install the event filter? How many widgets do you have at all? Maybe your filtered widget is obscured by another one so you get only some events.

jacek
25th January 2006, 23:00
but don't appear: MouseButtonPress = 2, MouseButtonRelease = 3, MouseButtonDblClick = 4,MouseMove = 5.
And how MouseMove is Called?
Don't you need to enable mouse tracking to get these?

mickey
26th January 2006, 09:42
Hi,
I link a loadButton to a SLOT:


MainfForm::load {
loadButton->installEventFilter(this->myWidget1);
loadButton->installEventFilter(this->myWidget2);
loadButton->installEventFilter(this->myWidget3);
......
QFileDialog fd;
QString te = fd.getOpenFileName(xdir.path()+"/"+" ....",
...........
}
Then:


bool MyWidget::eventFilter(QObject* obj, QEvent* e) {
cout << "filter\n";
cout << e->type() << endl;
if ( e->type() == QEvent::Move) {
cout << "filter out\n";
return TRUE;
}
return QGLWidget::eventFilter(obj,e);
}
I have 3 instance of MyWidget. The MouseMoveEvent in myWidget start when I choose a file "with doubleClick" in QFileDialog but printf inside QMouseEvent (in MyWidget) says
that only mouseMove start...:confused:

A hint? Thanks

fullmetalcoder
26th January 2006, 16:54
did you enable mouse tracking ??? Basically a widget records mose move only when a click occurs...

You can change that settings :


setMouseTracking(true) ;

mickey
26th January 2006, 19:22
did you enable mouse tracking ??? Basically a widget records mose move only when a click occurs...

You can change that settings :


setMouseTracking(true) ;

yes I proved.Don't change...I'm thinking that QmouseMoveEvent starts for some strange
reason.......

axeljaeger
27th January 2006, 22:10
Because you tried to contact me via skype and sending message failed, here's what I said there:

It would help if you'd provide a full working example with source as short as possible so I can try on my machine. I and others already suggested what could be the reason. If none of our tipps work, we have to see code. Often while you create such a like 100 LOC application, you solve the problem because the problem does not occur in the small testapp. Then you will compare what is different and hopefully find the reason. If not, post the programm

mickey
28th January 2006, 10:53
axeljaeger, thanks for reply...
I solved my problem but I don't understand why qMouseMoveEvent of MyWidget:: starts
starts after the code of loadButton():

void MainForm::loadbutton()
{
this->myWidget->installEventFilter(this->myWidget);
.............
printf("end mainform()LoadButt\n");
}
(With the printf above I see that QMouseMoveEvent of myWidget starts after (out) loadButtton(). But Why? I coded removeFilter at the end of loadbutton() but QmouseMoveEvent starts after and so don't filter it)


bool MyWidget::eventFilter(QObject* obj, QEvent* e) {
if (obj == this) {
cout << e->type() << endl;
if ( e->type() == QEvent::MouseMove) {
this->removeEventFilter(this);
cout << "filter out\n";
return TRUE;
}
else {
return QGLWidget::eventFilter(obj,e);
}
}
}
With cout << e->type I see all QEvent on myWidget and MouseMove runs only after the oadButton()...so I filter out only one (setMouseTracking is off) QMouseMove and I put removeFilter in the eventFilter (but I don't LIKE this....) .
What do you think of this?
thanks