Get Plain text from a QLabel with Rich text
Asked Answered
H

2

10

I have a QLabel that contains rich text.
I want to extract just the actual (visible) 'text' from the QLabel, and none of the code for formatting.
I essentially need a function similiar to the '.toPlainText' method of other Qt Widgets.

I can not simply call .text() and string manipulate away the html tags as suggested in this thread Get plain text from QString with HTML tags, since the returned QString contains all the <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0//EN" "http://www.w3.org/TR/REC-html40/strict.dtd"> nonsense.

How do I extract the plain text?

(I'm open to any method, even if indirect. eg; Pre-existing functions that convert html to plain text)

Thanks!

Specs:
python 2.7.2
PyQt4
Windows 7

Henghold answered 17/1, 2012 at 5:56 Comment(4)
I've not tried it myself, but it looks like QTextCodec is what you need (documentation at developer.qt.nokia.com/doc/qt-4.8/qtextcodec.html).Chui
RTF doesn't seem to be a supported encoding (unless I'm missing something). Never mind, I've found a messy work-aroundHenghold
Can't you just treat Qt Rich Text as html? The Qt document here (doc.qt.nokia.com/4.7-snapshot/qml-text.html) says: "Rich text is defined using HTML-style markup."Chui
Yea! (Is html supported by QTextCodec? I'm just going to assume so, since it's only logical if supported by the GUI framework. I'm not very knowledgeable in this whole 'mark-up',text format business! Sorry.)Henghold
V
21

Use a QTextDocument to do the conversion:

doc = QtGui.QTextDocument()
doc.setHtml(label.text())
text = doc.toPlainText()
Venn answered 17/1, 2012 at 18:16 Comment(1)
Oh yeah, bit more direct than my method. Embarrassed I missed that :|Henghold
H
2

Here's a messy work around (for python - PyQt)

def Extract_PlainText(label):
    Rtf_text = label.text()
    Temp_Obj = QtGui.QTextEdit()
    Temp_Obj.setText(Rtf_text)
    Plain_text = Temp_Obj.toPlainText()
    del Temp_Obj
    return Plain_text

Inspired by http://bytes.com/topic/net/answers/707370-convert-rtf-plain-text

Henghold answered 17/1, 2012 at 7:59 Comment(1)
Ofcourse the 'del Temp_Obj' is completely unnescessary, but I find it quaintHenghold

© 2022 - 2024 — McMap. All rights reserved.