Get Plain text from a QLabel with Rich text
Solution 1
Use a QTextDocument
to do the conversion:
doc = QtGui.QTextDocument()
doc.setHtml(label.text())
text = doc.toPlainText()
Solution 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
Comments
-
Anti Earth almost 2 years
I have a
QLabel
that contains rich text.
I want to extract just the actual (visible) 'text' from theQLabel
, and none of the code for formatting.
I essentially need a function similiar to the'.toPlainText'
method of otherQt 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 returnedQString
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 -
Anti Earth over 12 yearsOfcourse the 'del Temp_Obj' is completely unnescessary, but I find it quaint
-
Anti Earth over 12 yearsOh yeah, bit more direct than my method. Embarrassed I missed that :|