2

I am trying to add a link to a QTextBrowser and allow it to be clicked. I can get the link to display properly, but when I hover over it the mouse does not change at all (as if it were a URL to click) and you can't click it.

I have setReadOnly and setOpenExternalLinks to True for the QTextBrowser and properly formatted the html url.

self.playlist_txt = QtGui.QTextBrowser()
self.playlist_txt.setReadOnly(False)
self.playlist_txt.setOpenExternalLinks(True)

url_link = "https://google.com/"
html = '<a href="'+url_link+'">'+url_link+'</a>'
print html
self.playlist_txt.setHtml(html)

Output of "html":

<a href="https://google.com">https://google.com</a>
eyllanesc
  • 235,170
  • 19
  • 170
  • 241
Zak44
  • 341
  • 1
  • 5
  • 24
  • I'm not sure which Qt you're using (guessing Qt4 from `QtGui.QTextBrowser()`) but Qt5 documentation of `QTextBrowser` says this: This class extends QTextEdit (in read-only mode), adding some navigation functionality so that users can follow links in hypertext documents. – icwebndev Apr 26 '19 at 16:41
  • Using Qt4. I'm not sure what you are trying to say though? I made a link, I can't click it to open the link. Thats expected behavior. – Zak44 Apr 26 '19 at 18:06
  • 1
    @Zak44 remove `self.playlist_txt.setReadOnly(False)` – eyllanesc Apr 26 '19 at 18:41
  • Thank you @eyllanesc! That worked. I can give credit if you want to post the answer. – Zak44 Apr 26 '19 at 18:45
  • @Zak44 It is done – eyllanesc Apr 26 '19 at 18:47
  • @Zak44 It tells you exactly the same thing as the answer - you can navigate links from QTextBrowser only if it's in read-only mode. In other mode, it works as a QTextEdit. – icwebndev Apr 26 '19 at 19:24

1 Answers1

2

You must make the QTextBrowser be readonly so that the url can be clicked and open the url:

self.playlist_txt.setReadOnly(False)
eyllanesc
  • 235,170
  • 19
  • 170
  • 241