Frage

I have the following variable appended to 'QTextBrowser'. It does appear as a link, but when I click on it all the text in the 'QTextBrowser' disappears. All the function the 'anchorClicked' signal is connected to does is print something in the shell so that I know that the signal was received.

word = '<a href>' + '<span style="background-color:#C0C0C0">' + word + '</span>' +'</a>'

self.textBrowser.anchorClicked.connect(self.test)

def test(self,argv_1):
    print('!!!')
War es hilfreich?

Lösung

Probably what's happening is that the text browser is attempting navigate to the href specified in the anchor. But since the href is empty, it just shows a blank page.

If you want to stop automatic link navigation, try this:

self.textBrowser.setOpenLinks(False)

(NB: the anchorClicked signal will still be sent when the link is clicked).

Andere Tipps

You can also prevent this behaviour by calling self.textBrowser.setSource(QtCore.QUrl()) in the function connected to the anchorClicked signal (in your case test()).

For an example, see what I did in my answer to your other question here: https://stackoverflow.com/a/19475367/1994235

This allows you to still have some links that take you to other pages, and some that don't (you call the above line of code to prevent the page change, only when certain urls are passed to your function)

Use html2text to download the URL to matching directory for every link on the page. Reformat as HTML, adding headers and rewiring the links. Then do this recursively every time you click on a link and you effectively have a working web browser. The links will actually work. I would like to see someone do it in less than 3 pages if they can.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top