UrbKr
UrbKr

Reputation: 663

PySide/PyQt Is it possible to get the string of an anchor in QTextBrowser

In the following code is there anyway for python to tell what the 'word' in the middle of the href tags was when receiving the anchorClicked signal?

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

I tried with the sender() method, but the sender is QTextBrowser, not the string, so that didn't help.

Upvotes: 0

Views: 554

Answers (1)

ekhumoro
ekhumoro

Reputation: 120578

When you create the html, put the word in the href attribute:

 html = '<a href="%s"><span>%s</span></a>' % (word, word)

(NB: you may need to html-escape the words in case they include the characters <, >, &, or ").

You can then extract the word from the QUrl sent by anchorClicked:

 word = url.toString()

Alternatively, you could set the href to a key that can be used to look up the word data in a dict.

Upvotes: 1

Related Questions