Issue
I have two issues with how PyQt is formatting my QLabels
Issue 1: When hyperlinks are added it displays as if there were no newlines in the string. For the input text:
<a href = "https://www.google.co.uk/">https://www.google.co.uk/</a>
<a href = "https://www.google.co.uk/">https://www.google.co.uk/</a>
<a href = "https://www.google.co.uk/">https://www.google.co.uk/</a>
It's shown like this without newlines
Issue 2: Sometimes PyQt just doesn't even detect the 'a' tag this happens when the start of string is not a hyperlink but it is then followed by newlines with hyperlinks e.g. this input:
test
<a href = "https://www.google.co.uk/">https://www.google.co.uk/</a>
<a href = "https://www.google.co.uk/">https://www.google.co.uk/</a>
<a href = "https://www.google.co.uk/">https://www.google.co.uk/</a>
As you can see the newlines are properly shown but PyQt has no longer detected the hyperlinks
Solution
From the text property documentation of QLabel:
The text will be interpreted either as plain text or as rich text, depending on the text format setting; see setTextFormat(). The default setting is Qt::AutoText; i.e. QLabel will try to auto-detect the format of the text set.
The AutoText
flag can only make a guess using simple tag syntax checks (basic tags without arguments, such as <b>
, or document type declaration headers, like <html>
).
This is obviously done for performance reasons.
If you are sure that you're always setting rich text content, use the appropriate Qt.TextFormat
enum:
label.setTextFormat(QtCore.Qt.RichText)
Using the HTML-like syntax of rich text will obviously use the same basic concept HTML had since its birth, almost 30 years ago: line breaks between any word in the document (text or tag) are ignored, as much as multiple spaces are always considered as one.
So, if you want to add line breaks, you have to use the appropriate <br>
(or <br/>
for xhtml) tag.
Also remember that Qt rich text engine has a limited support, as described in the documentation about the Supported HTML Subset.
Answered By - musicamante
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.