Reputation: 4700
for example if you just set
self.textedit.setHtml("<b>Bold text</b>")
htmlCheck=self.textedit.toHtml()
hmtlCheck=
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0//EN" "http://www.w3.org/TR/REC-html40/strict.dtd">
<html><head><meta name="qrichtext" content="1" /><style type="text/css">
p, li { white-space: pre-wrap; }
</style></head><body style=" font-family:'MS Shell Dlg 2'; font-size:8.25pt;
font-weight:400; font-style:normal;">
<p style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px;
-qt-block-indent:0; text-indent:0px;"><span style=" font-weight:600;">Bold text</span>
</p>
</body></html>
Why can't I just only get my setted text from the first code line back? This, what I get back, is so bad for further editing... Imagine, I write a bigger text in this. I'd like to select text and make it bold, or make a list, and detect hyperlinks in real time... I don't know how to deal with it when there is so much garbage around my code that works alone, too. And there are afaik only the .toPlainText() and .toHtml() functions... The hyperlink-thing is really simple, I could just .setText(...) and .toPlainText() and run a regex each time over all the www.'s and http's. But I also want a dynamic list functionality or bold, maybe, and thus cannot use toPlainText()...
Has someone a good advise for me?
EDIT: This one here seems to work to set selected text bold, even through different paragraphs:
def setBold(self):
cur=self.textedit.textCursor()
if cur.hasSelection():
font=self.textedit.currentFont()
font.setWeight(QFont.Bold)
self.textedit.setCurrentFont(font)
Upvotes: 4
Views: 6598
Reputation: 69042
You can't get the exact text you set back because that's not what the QTextEditor internally stores. For that reason it's methods are called toHtml
and toPlainText
and not getHtml
, that should emphasize that what is returned is a representation of the editors content, not it's exact internal state.
If you want to interact with the editor, you should do it like described here:
QTextCursor
returned by the editors textCursor()
method to change selections or modify/insert text at the cursorUpvotes: 1