PyQt5 不正确的带有链接的标签格式

PyQt5 incorrect label formatting with links

关于 PyQt 如何格式化我的 QLabels,我有两个问题

问题 1: 添加超链接时,它显示为好像字符串中没有换行符。 对于输入文本:

<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>

没有换行是这样显示的

问题 2: 有时 PyQt 甚至没有检测到 'a' 标记 当字符串的开头不是超链接但随后是带有超链接的换行符,例如此输入:

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>

如您所见,换行符已正确显示,但 PyQt 不再检测到超链接

来自 QLabel 的 text property documentation

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.

AutoText 标志只能使用简单的标签语法检查(不带参数的基本标签,例如 <b>,或文档类型声明)进行 猜测 headers, 喜欢 <html>).
这显然是出于性能原因。

如果您确定始终设置富文本内容,请使用适当的 Qt.TextFormat 枚举:

    label.setTextFormat(QtCore.Qt.RichText)

使用富文本的 HTML-like 语法显然会使用相同的基本概念 HTML 自诞生以来,将近 30 年前: any[=38 之间的换行符=] 文档(文本或标签)中的单词 被忽略 ,多个空格始终被视为一个。

因此,如果您想添加换行符,您必须使用适当的 <br>(或 <br/> for xhtml)标签。

另请记住,Qt 富文本引擎具有 有限的 支持,如有关 Supported HTML Subset.

的文档中所述