如何在pyqt中的QTextEdit上创建超链接以启动邮件

Edu*_*yva 2 python pyqt qtextedit pyqt5

我想要一个超链接来在 QTextEdit 中启动邮件客户端。我尝试了此操作,但单击链接时没有任何反应:

self.text_area = QTextEdit()
self.text_area.setReadOnly(True)
self.text_area.setText(u'<p> Jhon Doe <a href='"'mailto:jhon@compay.com'"'>jhon@compay.com</a>  </p>')
self.text_area.setTextInteractionFlags(Qt.LinksAccessibleByMouse)
Run Code Online (Sandbox Code Playgroud)

eyl*_*esc 5

Use QTextBrowser,这是一个专门的类,它提供了具有超文本导航功能的富文本浏览器,该类继承自QTextEdit,因此它至少具有相同的QTextEdit功能。

import sys

from PyQt5.QtWidgets import QApplication, QTextBrowser

if __name__ == '__main__':
    app = QApplication(sys.argv)
    text_area = QTextBrowser()
    text_area.setText(u'<p> Jhon Doe <a href='"'mailto:jhon@compay.com'"'>jhon@compay.com</a>  </p>')
    text_area.setOpenExternalLinks(True)
    text_area.show()
    sys.exit(app.exec_())
Run Code Online (Sandbox Code Playgroud)