Question

I'm trying to implement the following functionality based on QPlainTextEdit - by default it should show a "please enter something here" message, on click, it would vanish and allow editing.

To do so I need to intercept whether the widget has been clicked. Can I do so without sub classing QPlainTextEdit?

Can the parent's widget onMousePressEvent obtain which child's widget the event belongs to?

Was it helpful?

Solution

Can I do so without sub classing QPlainTextEdit?

Yes, you can implement an event filter:

http://qt-project.org/doc/qt-4.8/eventsandfilters.html#event-filters

Essentially, you can filter out the mouse events destined for the plain text edit. Whenever your filter encounters a mouse press event, you can clear the contents of the plain text edit.

Can the parent's widget onMousePressEvent obtain which child's widget the event belongs to?

Not without event filtering. Qt's event propagation system sends events to child widgets first and then only to parent widgets if the child widget does not accept the event.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top