3

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)
user2366975
  • 4,350
  • 9
  • 47
  • 87

1 Answers1

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
mata
  • 67,110
  • 10
  • 163
  • 162
  • How do i modify the html file if the selected text goes over several paragraphs

    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.

    – user2366975 Aug 20 '13 at 18:32
  • 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`](http://pyqt.sourceforge.net/Docs/PyQt4/qtextedit.html#setCurrentFont) method, which will change the font of the current selection (use `selectAll` or the QTextCursor to set the selection.) – mata Aug 20 '13 at 18:51
  • 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. – user2366975 Aug 20 '13 at 21:02
  • If you want to make selected text bold, you can use `font=editor.currentFont(); font.setWeight(QFont.Bold); editor.setFont(font)`. – mata Aug 20 '13 at 21:24
  • 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. – user2366975 Aug 20 '13 at 21:42
  • Ah here I've found it: http://stackoverflow.com/questions/12024596/qtextedit-change-font It is setCurrentFont, not setFont. Woah. nice, thank you! Now I just have to look how I set it non-bold again if i click it twice... – user2366975 Aug 20 '13 at 21:51