PDA

View Full Version : QTextEdit eventfilter MouseButtonDblClick



Henry Blue Heeler
9th April 2015, 01:43
The eventfilter() for QTextEdit is not catching the MouseButtonDblClick from the mouse button. It is catching it from the mouse wheel!
Also, everything works fine if I derive QTextEdit and reimplement void mouseDoubleClickEvent(QMouseEvent * event);
I'd prefer to not derive QTextEdit.

Is this behavior a Windows feature? Windows 7, Qt 5.1.1

wysota
9th April 2015, 07:39
Install the filter on the viewport rather than the text edit itself.

Henry Blue Heeler
9th April 2015, 20:05
I did install it on the text edit's viewport, to no avail. I should have mentioned that.

wysota
9th April 2015, 20:45
Please prepare a minimal compilable example reproducing the problem.

Henry Blue Heeler
9th April 2015, 21:58
Pilot error! The event filter:


textEdit->viewport()->installEventFilter(this);
...
bool Dialog::eventFilter(QObject* obj, QEvent* event)
{
if( obj == textEdit ) // Wrong!! since the event filter was installed on the viewport, we need the parent
{
if (event->type() == QEvent::MouseButtonDblClick)
{
}
}
}

bool Dialog::eventFilter(QObject* obj, QEvent* event)
{
if( obj->parent()== textEdit ) // Correct!
{
if (event->type() == QEvent::MouseButtonDblClick)
{
}
}
}

wysota
9th April 2015, 22:58
I suggest to test against obj == textEdit->viewport() rather than obj->parent() == textEdit.

Henry Blue Heeler
9th April 2015, 23:07
Either works. Are you thinking perhaps the viewport's parent may change?
I can see that obj == textEdit->viewport() has more certainty.
Regardless, thanks again for the help.

wysota
10th April 2015, 06:05
Either works. Are you thinking perhaps the viewport's parent may change?
I am thinking that you are testing against the object you really want to test against instead of testing a relation which may be true for more than one object. If you are expecting "4", test against "4" and not "an even number".

Henry Blue Heeler
11th April 2015, 02:13
Yes, the object (viewport) could have multiple parents (unlikely in this scenario), but I get the nuance. Better practice, per se. Thx.

wysota
11th April 2015, 07:33
Yes, the object (viewport) could have multiple parents
No. The text edit may have (and indeed has) multiple children.

Henry Blue Heeler
11th April 2015, 22:12
No. The text edit may have (and indeed has) multiple children.

Good point! Thx.