[PyKDE] QTextEdit and clickable links

Sundance sundance at ierne.eu.org
Mon Sep 29 16:06:00 BST 2003


I heard Sundance said:

> Is there any way to do it that I may not be aware of?

Okay, found it. Here's how I did, as a reference in case someone else 
has the same problem later on. :)

The standard behavior of a QTextEdit's content widget (that you obtain 
with the viewport() method), is to have mouseTracking enabled. This is 
because QTextEdit is a specialisation of QScrollView. There is thus an 
event sent everytime the mouse cursor moves over the QTextEdit. This 
event can be caught by overloading contentsMouseMoveEvent().

In there, you get the current position of the mouse pointer, and see if 
there's a link at this position with QTextEdit's anchorAt method. This 
allows you to set the mouse cursor to a pointing hand when you're on a 
link. Be sure to set the mouse cursor for the viewport() and not the 
QTextEdit itself, since it's in the content, in the viewport(), that 
you want it to happen.

And catching clicks is, likewise, a matter of overloading 
contentsMousePressEvent() and contentsMouseReleaseEvent(). Don't forget 
to forward the event to the parent class when you're done with it!

That's it. I feared overloading a MouseMove event in Python would be 
awfully slow, but it seems to work fine.

Aaand now, for bonus points, any idea about how to make links underlined 
only when the mouse pointer is over it?

-- S.




More information about the PyQt mailing list