[PyQt] Is it possible to override/extend QTextCursor?

r nbs.public at gmail.com
Mon Feb 9 01:48:03 GMT 2009


[This is a several months old thread]

On Sun, Feb 8, 2009 at 8:56 PM, David Boddie <david at boddie.org.uk> wrote:
>
> [Reimplementing QTextCursor.insertText() methods]

[...]
> What are you using QTextCursor for, and in what way do you need to extend it?

I'm trying to intercept paste "events" for the QTextEdit widget so
that when the middle mouse button is pressed (Unix) the text is pasted
at the last cursor location (not one under the mouse pointer). Also, I
would like to process pasted text just as if it was typed manually.
All this is needed to make QTextEdit (actually QPlainTextEdit) behave
similarly to a text console.

AFAIK there is no "paste" signal or event for QTextEdit which I could
handle directly. So either I handle all keyboard and mouse events
manually (which would mean I have to reimplement context menu, input
methods etc. from scratch) or I intercept operations on the document
at the cursor level (which seamed to be a good idea until I tried it).

> I suspect that you would be better off trying to respond to changes in the
> document instead, probably by handling the signals emitted by each
> QTextDocument instance. I don't think that the rich text classes were
> designed to be extended in this way - most of the flexibility is built
> into the text layout engine, but this isn't exactly easy to extend for
> most people.

I haven't tried using QTextDocument signals yet. This is because I
wanted to intercept changes to the document before they happen, not
after, so that I don't have to revert the changes, cope with circular
event loops, visual artefacts (flicker?) etc. Not sure if those are
real problems though.

Another potential issue is whether I can reimplement QTextDocument - I
wouldn't be surprised if I hit same problem as with QTextCursor.

Regards,
-r.


More information about the PyQt mailing list