-1

This is a small sample code for an application using socket. Аfter clicking on a link 'textlink' and adding untagged text by pressing 'add text' button, all subsequent text become hyperlink. By pressing gethtml button you can get html code in command line. Ho can i turn it off?

PS Adding a tag to every new line is not a solution.


import sys
from PyQt5.QtWidgets import *


class MainWindow(QMainWindow):

    def __init__(self):
        QMainWindow.__init__(self)
        self.resize(500, 500)
        self.browser = QTextBrowser(self)
        self.browser.resize(500, 300)
        self.btn = QPushButton('get html print', self)
        self.btn.move(250, 400)
        self.btn.clicked.connect(self.gethtml)
        self.btn2 = QPushButton('add text', self)
        self.btn2.move(150, 400)
        self.btn2.clicked.connect(self.addtxt)
        self.browser.setOpenLinks(False)
        self.browser.anchorClicked.connect(self.anchor_clicked)

    def gethtml(self):
        print(self.browser.toHtml())

    def addtxt(self):
        self.browser.append('sample text 1')
        self.browser.append('some text: <a href="http://link.com">textlink</a>')
        self.browser.append('sample text 2')

    def anchor_clicked(self):
        pass


if __name__ == '__main__':
    app = QApplication(sys.argv)
    dlgMain = MainWindow()
    dlgMain.show()
    sys.exit(app.exec_())

Before i clicked anchor: enter image description here

After i clicked anchor: enter image description here

Alexandr
  • 21
  • 4
  • 1
    so, what's the question? – rv.kvetch Sep 02 '22 at 15:27
  • Maybe how to make qtextbrowser not to add link to every new line after i click to anchor?? – Alexandr Sep 02 '22 at 15:54
  • 1
    @Alexandr Sorry, but your post is really unclear. Try to explain yourself better. – musicamante Sep 02 '22 at 15:58
  • @musicamante i added some pics. is it cleer now? thanks for reply – Alexandr Sep 02 '22 at 16:13
  • @Alexandr Sorry, but while images can be useful, they aren't enough without a *clear description* of the problem and the expected result. – musicamante Sep 02 '22 at 16:28
  • @Alexandr Ok, now it's more clear, thanks. Just remember: 1. always notify when you do any changes, as we cannot follow all posts and cannot always check the editing history for updates; 2. *always* ensure that your question is clear by putting yourself in other's shoes: people knows nothing of your problem, and you must try to explain it to them very carefully (read about [rubber duck debugging](//en.wikipedia.org/wiki/Rubber_duck_debugging), and imagine we're the duck), also considering that most people doesn't speak English as their primary language. – musicamante Sep 02 '22 at 22:17

1 Answers1

1

This is actually the expected behavior. As explained in the documentation of append():

Note: The new paragraph appended will have the same character format and block format as the current paragraph, determined by the position of the cursor.

This might be unintuitive for QTextBrowser, since it doesn't show the text cursor (the "caret"), but clicking on a QTextBrowser does change the cursor position, exactly like it would if you used setReadOnly(False) or by using a standard QTextEdit.

The solution is to always move the text cursor to the end before calling append(). Remember that in order to make the actual text edit/browser cursor move, you must call setTextCursor() again after moving its position.

    def addtxt(self):
        # get the QTextCursor of the widget's document
        cursor = self.browser.textCursor()
        # move it to the end of the document
        cursor.movePosition(cursor.End)
        # *restore* the text cursor on the widget
        self.browser.setTextCursor(cursor)

        self.browser.append('sample text 1')
        self.browser.append('some text: <a href="http://link.com">textlink</a>')
        self.browser.append('sample text 2')
musicamante
  • 41,230
  • 6
  • 33
  • 58