如何将URL链接嵌入到QLabel中

15

点击QLabel应该打开指定的URL链接的默认网络浏览器,但目前还未发生。为什么呢? 第二个问题。是否可以将标签字体的默认蓝色改为其他颜色?

输入图片说明

class Widget(QtGui.QWidget):
    def __init__(self, *args):
        QtGui.QWidget.__init__(self, *args)
        vLayout=QtGui.QVBoxLayout(self)
        self.setLayout(vLayout)
        urlLink="<a href=\"http://www.google.com\">'Click this link to go to Google'</a>" 
        label=QtGui.QLabel(self)
        label.setText(urlLink)
        vLayout.addWidget(label)


if __name__ == "__main__":
    app = QtGui.QApplication(sys.argv)
    w = Widget()
    w.show()
    sys.exit(app.exec_())
2个回答

25

1
谢谢Ekhumoro!这里是黑色的:urlLink=" <a href=\"http://www.google.com\"> <font face=verdana size=12 color=black> 这是一个链接</font> </a>" - alphanumeric
1
还应该注意,setTextInteractionFlags 必须设置 LinksAccessibleByMouse。https://doc-snapshots.qt.io/qtforpython-5.15/PySide2/QtCore/Qt.html?highlight=textselectablebymouse#PySide2.QtCore.PySide2.QtCore.Qt.TextInteractionFlag - Lorem Ipsum

0

在 Qt Designer 中,

  1. 确保所选的标签对象包含链接,
  2. 在 Property Editor 中的 QLabel 组中找到 openExternalLinks 属性(您可以在 Property Editor 过滤字段中键入 open),
  3. 将属性 openExternalLinks 设置为 True(勾选)。[默认情况下此属性设置为 False]。

Qt Designer Property Editor


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接