pythonqtpyside2qtextbrowser

Pyside2 QTextBrowser overwriteMode does not replace old text


I'm using the PySide2 package and found that the overwriteMode won't work in my code. Here is what I wrote:

self.ui.textBrowser.setOverwriteMode(True)
self.ui.textBrowser.insertPlainText('test1\ntest2\ntest3')
self.ui.textBrowser.moveCursor(QTextCursor.Start)
self.ui.textBrowser.insertPlainText('hhh')

Complete source code

I used Qt-Creator to design my GUI and my settings for the QTextBrowser looks like that:

QTextBrowser settings

I got the following output:

Output

As you can see, the first line should be 'hhht1' but it gives 'hhhtest1'.

I'm testing this with PySide2 version 5.11.2 on a Windows 10.

I am new to Qt and can anyone please help me out? What am I missing? Thank you!


Solution

  • According to the documentation of the overwriteMode property:

    This property holds whether text entered by the user will overwrite existing text

    As with many text editors, the text editor widget can be configured to insert or overwrite existing text with new text entered by the user.

    So apparently the overwriteMode affects only text entered by the user.

    You probably need to do something like:

    text = 'test1\ntest2\ntest3'
    self.ui.textBrowser.setPlainText(text)
    inserted_text = 'hhh'
    text = inserted_text + text[len(inserted_text):]
    self.ui.textBrowser.setPlainText(text)