[PyQt] Please tell me if I have this straight...

Hans-Peter Jansen hpj at urpla.net
Fri Aug 5 21:20:05 BST 2011


On Friday 05 August 2011, 20:27:51 David Cortesi wrote:
> I present a document to a user in a QPlainTextEdit widget. The widget
> nicely handles most editing functions, but there is a unique
> operation I want to provide. When the user selects Edit > BLEEP, the
> app is supposed to BLEEP the document.
>
> BLEEPing is not something Qt does, I have to implement BLEEP with
> native Python code. So when the user says BLEEP it, I must:
>
> 1. Use QPlainTextEdit toPlainText() to get the document text as a
> QString
>
> 2. Use QString toUTF8() to get Utf-8 text that Python can use

No, you just convert the QString to unicode.

> 3. Apply my Python logic to BLEEP the text
>
> 4. Use QString fromUtf8() to return to a QString

See above.

> 5. Use QPlainTextEdit fromPlainText() to replace the document with
> the BLEEPed contents.
>
> Does this sound right?
>
> Can PyQt do #2 and #4 automagically?

Sure, just do

import sip
sip.setapi('QString', 2)

before any PyQt imports, which does convert QString to unicode behind 
the scenes. Anyway, no need to encode/decode utf-8 strings here.

> And what are the performance implications when a document might have
> one or two megabytes of text?

I guess, it mostly depends on the performance of your bleep algorithm. 
The rest is operating with compiled code, which should be sufficient.

Pete


More information about the PyQt mailing list