The font setting becomes invalid after deleting the current line of a textBrowser
I have a button and the slot connected to it is:
def add_a(self): lastLine = self.main_ui.textBrowser_3.textCursor() lastLine.select(QTextCursor.LineUnderCursor) lastLine.removeSelectedText() self.aa = str(self.aa + 'a') self.main_ui.textBrowser_3.append(self.aa)
Every time a new line is appended to the textBrowser, the current line is deleted first. The initial self.aa is 'a'. I found the font is changed after the slot is called once.
The first result:
Then the font looks different ever.
Seems the font setting is invalid. How to solve this? I did not change other things, just keep clicking the button.
Do I see correctly that you empty the text edit before adding the new line ?
@SGaist Yes, I empty the current line before adding the new line.
I did another test with the above code. If the textBrowser is empty at the begining, then the text did not move to the second line and the font changed. But if the textBrowser has some text at the begining, then the new text can move to the second line and the font is OK. Do you know why?
IIRC, the fact that the whole text is nuked has the side effect of also deleting the style associated with it.
What do you get if you select the current text and overwrite it ?
How to overwrite the text?
lastLine = self.main_ui.textBrowser_3.textCursor() lastLine.select(QTextCursor.LineUnderCursor) self.aa += 'a' lastLine.insertText(self.aa)
I tried the above code, but it's useless. The font format is still not right.
One workaround: change the selection so that it's all the letter except the first one, replace the text, delete the first char.