5

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)

1 Answer 1

1

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:

  • use the methods designed to edit the (selected) content
  • use a QTextCursor returned by the editors textCursor() method to change selections or modify/insert text at the cursor
Sign up to request clarification or add additional context in comments.

6 Comments

How do i modify the html file if the selected text goes over several paragraphs <p> that always occur after a line break Enter? And for example, if I want to change the fontsize... I replace "font-family:'MS Shell Dlg 2'" vs. "font-family:'Arial'" and set the new html with the replaced font. Seems to work. But after I made a 2nd toHtml() it says "font-family:'MS Shell Dlg 2'" again! I don't understand this.
The font isn't preserved on the body's style, but pushed down to the children. The rendering will be the same. If you want to change the font, use the setCurrentFont method, which will change the font of the current selection (use selectAll or the QTextCursor to set the selection.)
Hm so now i tested it with cur=self.textedit.textCursor(), and as I said if I for example want to make text bold, and the selectedText goes over some paragraphs, it does not work because it can't be replaced, because it does not match (there are html tags in between)... Really curious how this should go.
If you want to make selected text bold, you can use font=editor.currentFont(); font.setWeight(QFont.Bold); editor.setFont(font).
The first command works, 2nd don't know and the third does nothing. How should it, don't I have to use setHtml() with tags? I have edited my question that one can see the code of you that ive tried. Does not work.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.